1 use std::{
2 net::{TcpListener, TcpStream},
3 thread::spawn,
4 };
5
6 use log::*;
7 use tungstenite::{accept, handshake::HandshakeRole, Error, HandshakeError, Message, Result};
8
must_not_block<Role: HandshakeRole>(err: HandshakeError<Role>) -> Error9 fn must_not_block<Role: HandshakeRole>(err: HandshakeError<Role>) -> Error {
10 match err {
11 HandshakeError::Interrupted(_) => panic!("Bug: blocking socket would block"),
12 HandshakeError::Failure(f) => f,
13 }
14 }
15
handle_client(stream: TcpStream) -> Result<()>16 fn handle_client(stream: TcpStream) -> Result<()> {
17 let mut socket = accept(stream).map_err(must_not_block)?;
18 info!("Running test");
19 loop {
20 match socket.read()? {
21 msg @ Message::Text(_) | msg @ Message::Binary(_) => {
22 socket.send(msg)?;
23 }
24 Message::Ping(_) | Message::Pong(_) | Message::Close(_) | Message::Frame(_) => {}
25 }
26 }
27 }
28
main()29 fn main() {
30 env_logger::init();
31
32 let server = TcpListener::bind("127.0.0.1:9002").unwrap();
33
34 for stream in server.incoming() {
35 spawn(move || match stream {
36 Ok(stream) => {
37 if let Err(err) = handle_client(stream) {
38 match err {
39 Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => (),
40 e => error!("test: {}", e),
41 }
42 }
43 }
44 Err(e) => error!("Error accepting stream: {}", e),
45 });
46 }
47 }
48