/// 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#"
"#; // 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() }