Giter Site home page Giter Site logo

ws2's Introduction

Very Simple Websocket For Rust

ws2 is a very easy to use WebSocket server & client for Rust, builds on ws crate. it was designed for production, the receive timeout provides a non-blocking way.

Server Example

use log2::*;
use ws2::{Pod, WebSocket};

struct Worker;

impl ws2::Handler for Worker {
    fn on_open(&mut self, ws: &WebSocket) -> Pod {
        info!("on open: {ws}");
        Ok(())
    }

    fn on_close(&mut self, ws: &WebSocket) -> Pod {
        info!("on close: {ws}");
        Ok(())
    }

    fn on_message(&mut self, ws: &WebSocket, msg: String) -> Pod {
        info!("on message: {msg}, {ws}");
        let echo = format!("echo: {msg}");
        let n = ws.send(echo);
        Ok(n?)
    }
}

fn main() -> Pod {
    let _log2 = log2::start();
    let address = "127.0.0.1:3125";
    let mut worker = Worker {};

    info!("listen on: {address}");
    let mut server = ws2::listen(address)?;

    loop {
        let _ = server.process(&mut worker, 0.5);
        // do other stuff
    }
}

Client Example

The websocket client was designed as out-of-the-box, it will auto reconnect every 3s.

use log2::*;
use ws2::{Pod, WebSocket};

struct Worker;

impl ws2::Handler for Worker {
    fn on_open(&mut self, ws: &WebSocket) -> Pod {
        // ws.send("Hello World")?;
        info!("on open: {ws}");
        Ok(())
    }

    fn on_close(&mut self, ws: &WebSocket) -> Pod {
        info!("on close: {ws}");
        Ok(())
    }

    fn on_message(&mut self, ws: &WebSocket, msg: String) -> Pod {
        info!("on message: {msg}, {ws}");
        Ok(())
    }
}

fn main() -> Pod {
    let _log2 = log2::start();
    let url = "wss://stream.binance.com:9443/ws/btcusdt@miniTicker";
    let mut client = ws2::connect(url);
    let mut workder = Worker {};

    loop {
        let _ = client.process(&mut workder, 0.5);
        // do other stuff
    }
}

ws2's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

fermankamil

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.