Crates.io | websocket-server-async |
lib.rs | websocket-server-async |
version | 0.3.0 |
source | src |
created_at | 2022-02-04 12:31:52.66995 |
updated_at | 2024-01-18 08:17:04.166837 |
description | rust websocket server tokio frame. |
homepage | |
repository | https://github.com/luyikk/websocket-server-async |
max_upload_size | |
id | 526762 |
size | 51,886 |
#rust websocket tokio server frame.
use anyhow::Result;
use futures_util::StreamExt;
use log::*;
use websocket_server_async::{Builder, IPeer, IWebSocketServer};
#[tokio::main]
async fn main() -> Result<()> {
env_logger::Builder::new()
.filter_level(log::LevelFilter::Debug)
.init();
let websocket_server = Builder::new("0.0.0.0:8888")
.set_connect_event(|addr| {
info!("{} connect", addr);
true
})
.set_input_event(|mut reader, peer, _| async move {
while let Some(msg) = reader.next().await {
let msg = msg?;
if msg.is_text() || msg.is_binary() {
peer.send_message(msg).await?;
}
}
Ok(())
})
.build()
.await;
websocket_server.start_block(()).await?;
Ok(())
}