-
Notifications
You must be signed in to change notification settings - Fork 74
/
websocket.rs
30 lines (25 loc) · 829 Bytes
/
websocket.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#[macro_use]
extern crate may;
extern crate native_tls;
extern crate tungstenite;
use may::net::TcpListener;
use tungstenite::accept;
fn main() {
let handler = go!(move || {
let listener = TcpListener::bind(("0.0.0.0", 8080)).unwrap();
for stream in listener.incoming() {
go!(move || {
let mut websocket = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read().unwrap();
// Just echo back everything that the client sent to us
if msg.is_binary() || msg.is_text() {
websocket.send(msg).unwrap();
}
}
});
}
});
println!("Websocket server running on ws://0.0.0.0:8080");
handler.join().unwrap();
}