4 releases

0.2.2 Jan 23, 2022
0.2.1 Jan 6, 2021
0.2.0 Jan 6, 2021
0.1.0 Jan 6, 2021

#18 in WebSocket

Download history 15/week @ 2022-03-13 24/week @ 2022-03-20 4/week @ 2022-03-27 3/week @ 2022-04-03 4/week @ 2022-04-10 9/week @ 2022-04-17 28/week @ 2022-04-24 25/week @ 2022-05-01 44/week @ 2022-05-08 99/week @ 2022-05-15 33/week @ 2022-05-22 41/week @ 2022-05-29 54/week @ 2022-06-05 12/week @ 2022-06-12 20/week @ 2022-06-19 13/week @ 2022-06-26

103 downloads per month

MIT license

24KB
255 lines

wasm-sockets

wasm-sockets is a WASM only rust websocket library primarily designed for creating games.

This crate offers 2 (wasm-only) websocket clients. The first client offered is the EventClient. This client is event based and gives you the most control.

use console_error_panic_hook;
use console_log;
use log::{error, info, Level};
use std::panic;
use wasm_sockets::{self, WebSocketError};

fn main() -> Result<(), WebSocketError> {
    panic::set_hook(Box::new(console_error_panic_hook::hook));
    // console_log and log macros are used instead of println!
    // so that messages can be seen in the browser console
    console_log::init_with_level(Level::Trace).expect("Failed to enable logging");
    info!("Creating connection");

    let mut client = wasm_sockets::EventClient::new("wss://ws.ifelse.io")?;
    client.set_on_error(Some(Box::new(|error| {
        error!("{:#?}", error);
    })));
    client.set_on_connection(Some(Box::new(|client: &wasm_sockets::EventClient| {
        info!("{:#?}", client.status);
        info!("Sending message...");
        client.send_string("Hello, World!").unwrap();
        client.send_binary(vec![20]).unwrap();
    })));
    client.set_on_close(Some(Box::new(|| {
        info!("Connection closed");
    })));
    client.set_on_message(Some(Box::new(
        |client: &wasm_sockets::EventClient, message: wasm_sockets::Message| {
            info!("New Message: {:#?}", message);
        },
    )));

    info!("Connection successfully created");
    Ok(())
}

The second client offered is the PollingClient. This client is ideal for games, because it is designed to be used with a loop. This client is also much simpler than the EventClient. However, you can access the main EventClient that it is using if you want access to lower level control.

use console_error_panic_hook;
use log::{info, Level};
use std::cell::RefCell;
use std::panic;
use std::rc::Rc;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use wasm_sockets::{self, ConnectionStatus, WebSocketError};

fn main() -> Result<(), WebSocketError> {
    panic::set_hook(Box::new(console_error_panic_hook::hook));
    // console_log and log macros are used instead of println!
    // so that messages can be seen in the browser console
    console_log::init_with_level(Level::Trace).expect("Failed to enable logging");
    info!("Creating connection");

    // Client is wrapped in an Rc<RefCell<>> so it can be used within setInterval
    // This isn't required when being used within a game engine
    let client = Rc::new(RefCell::new(wasm_sockets::PollingClient::new(
        "wss://ws.ifelse.io",
    )?));

    let f = Closure::wrap(Box::new(move || {
        if client.borrow().status() == ConnectionStatus::Connected {
            info!("Sending message");
            client.borrow().send_string("Hello, World!").unwrap();
        }
        // receive() gives you all new websocket messages since receive() was last called
        info!("New messages: {:#?}", client.borrow_mut().receive());
    }) as Box<dyn Fn()>);

    // Start non-blocking game loop
    setInterval(&f, 100);
    f.forget();

    Ok(())
}
// Bind setInterval to make a basic game loop
#[wasm_bindgen]
extern "C" {
    fn setInterval(closure: &Closure<dyn Fn()>, time: u32) -> i32;
}

Dependencies

~0.3–2.6MB
~52K SLoC