|
|
@ -2,25 +2,34 @@ use async_std::net::{SocketAddr, TcpListener, TcpStream}; |
|
|
|
use async_tungstenite::accept_async; |
|
|
|
use async_tungstenite::accept_async; |
|
|
|
use futures::{SinkExt, StreamExt}; |
|
|
|
use futures::{SinkExt, StreamExt}; |
|
|
|
use log::*; |
|
|
|
use log::*; |
|
|
|
|
|
|
|
use tungstenite::Result; |
|
|
|
|
|
|
|
|
|
|
|
async fn accept_connection(peer: SocketAddr, stream: TcpStream) { |
|
|
|
async fn accept_connection(peer: SocketAddr, stream: TcpStream) { |
|
|
|
|
|
|
|
if let Err(err) = handle_connection(peer, stream).await { |
|
|
|
|
|
|
|
error!("Error processing connection: {}", err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn handle_connection(peer: SocketAddr, stream: TcpStream) -> Result<()> { |
|
|
|
let mut ws_stream = accept_async(stream).await.expect("Failed to accept"); |
|
|
|
let mut ws_stream = accept_async(stream).await.expect("Failed to accept"); |
|
|
|
|
|
|
|
|
|
|
|
info!("New WebSocket connection: {}", peer); |
|
|
|
info!("New WebSocket connection: {}", peer); |
|
|
|
|
|
|
|
|
|
|
|
while let Some(msg) = ws_stream.next().await { |
|
|
|
while let Some(msg) = ws_stream.next().await { |
|
|
|
let msg = msg.expect("Failed to get request"); |
|
|
|
let msg = msg?; |
|
|
|
if msg.is_text() || msg.is_binary() { |
|
|
|
if msg.is_text() || msg.is_binary() { |
|
|
|
ws_stream.send(msg).await.expect("Failed to send response"); |
|
|
|
ws_stream.send(msg).await?; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async fn run() { |
|
|
|
async fn run() { |
|
|
|
env_logger::init(); |
|
|
|
env_logger::init(); |
|
|
|
|
|
|
|
|
|
|
|
let addr = "127.0.0.1:9002"; |
|
|
|
let addr = "127.0.0.1:9002"; |
|
|
|
let listener = TcpListener::bind(&addr).await.unwrap(); |
|
|
|
let listener = TcpListener::bind(&addr).await.expect("Can't listen"); |
|
|
|
info!("Listening on: {}", addr); |
|
|
|
info!("Listening on: {}", addr); |
|
|
|
|
|
|
|
|
|
|
|
while let Ok((stream, _)) = listener.accept().await { |
|
|
|
while let Ok((stream, _)) = listener.accept().await { |
|
|
|