~starkingdoms/starkingdoms

ref: 9060a0fa0befd06174f514bdcefdaf8797b3beac starkingdoms/client/src/lib.rs -rw-r--r-- 1.1 KiB
9060a0fa — core hell pt3 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::error::Error;
use futures::StreamExt;
use log::{debug, error, info, Level};
use tokio_tungstenite::connect_async;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern {
    pub fn alert(s: &str);
}

#[wasm_bindgen]
pub fn send_chat(chat: &str) {
    info!("sending chat: {}", chat);
}

#[wasm_bindgen]
pub async fn rust_init(gateway: &str, username: &str) {
    console_log::init_with_level(Level::Debug).unwrap();

    info!("Logger setup successfully");

    match init(gateway, username).await {
        Ok(_) => (),
        Err(e) => {
            error!("Error initializing gateway client: {}", e);
            return;
        }
    }

    info!("Gateway client initialized successfully");
}

pub async fn init(gateway: &str, username: &str) -> Result<(), Box<dyn Error>> {
    info!("FAST CONNECT: {}", gateway);
    let gateway_url = url::Url::parse(gateway)?;
    debug!("Gateway URL parsed");
    let (ws_stream, _) = connect_async(gateway_url).await?;
    debug!("Connected to gateway socket");
    let (tx, rx) = ws_stream.split();
    debug!("Split stream, handshaking with server");

    Ok(())
}