summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ws/examples/remote_addr.rs
blob: d32e17af25c9e599003bd6264bda4854c918b41d (plain)
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
/// Example showing how to obtain the ip address of the client, where possible.
extern crate ws;

struct Server {
    ws: ws::Sender,
}

impl ws::Handler for Server {
    fn on_open(&mut self, shake: ws::Handshake) -> ws::Result<()> {
        if let Some(ip_addr) = shake.remote_addr()? {
            println!("Connection opened from {}.", ip_addr)
        } else {
            println!("Unable to obtain client's IP address.")
        }
        Ok(())
    }

    fn on_message(&mut self, _: ws::Message) -> ws::Result<()> {
        self.ws.close(ws::CloseCode::Normal)
    }
}

fn main() {
    ws::listen("127.0.0.1:3012", |out| Server { ws: out }).unwrap()
}