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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/// An example of a chat web application server
extern crate ws;
use ws::{listen, Handler, Message, Request, Response, Result, Sender};
// This can be read from a file
static INDEX_HTML: &'static [u8] = br#"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<pre id="messages"></pre>
<form id="form">
<input type="text" id="msg">
<input type="submit" value="Send">
</form>
<script>
var socket = new WebSocket("ws://" + window.location.host + "/ws");
socket.onmessage = function (event) {
var messages = document.getElementById("messages");
messages.append(event.data + "\n");
};
var form = document.getElementById("form");
form.addEventListener('submit', function (event) {
event.preventDefault();
var input = document.getElementById("msg");
socket.send(input.value);
input.value = "";
});
</script>
</body>
</html>
"#;
// Server web application handler
struct Server {
out: Sender,
}
impl Handler for Server {
//
fn on_request(&mut self, req: &Request) -> Result<(Response)> {
// Using multiple handlers is better (see router example)
match req.resource() {
// The default trait implementation
"/ws" => Response::from_request(req),
// Create a custom response
"/" => Ok(Response::new(200, "OK", INDEX_HTML.to_vec())),
_ => Ok(Response::new(404, "Not Found", b"404 - Not Found".to_vec())),
}
}
// Handle messages recieved in the websocket (in this case, only on /ws)
fn on_message(&mut self, msg: Message) -> Result<()> {
// Broadcast to all connections
self.out.broadcast(msg)
}
}
fn main() {
// Listen on an address and call the closure for each connection
listen("127.0.0.1:8000", |out| Server { out }).unwrap()
}
|