1use std::vec::Vec;
2use std::rc::Rc;
3use std::cell::RefCell;
4use ws::{listen, Sender, Handler, Message, Handshake, CloseCode, Result};
5use serde_json;
6#[derive(Serialize, Deserialize)]
9struct MessageJSON {
10 #[serde(rename = "type")]
11 type_name: String,
12 content: String,
13 from: usize,
14 to: usize,
15}
16
17type Connections = Rc<RefCell<Vec<Sender>>>;
18struct Server {
19 ws: Sender,
20 connections: Connections,
21}
22
23impl Handler for Server {
24 fn on_open(&mut self, _: Handshake) -> Result<()> {
25 let open_message = MessageJSON {
26 type_name: String::from("open"),
27 content: format!(""),
28 from: usize::from(self.ws.token()),
29 to: usize::from(self.ws.token()),
30 };
31
32 match serde_json::to_string(&open_message) {
33 Ok(s) => {
34 let msg = Message::text(s);
35 self.ws.send(msg)?;
36 },
37 Err(e) => println!("{:?}", e)
38 };
39
40 let mut connections = self.connections.borrow_mut();
41 for connection in connections.iter() {
42 let connected_message = MessageJSON {
43 type_name: String::from("connected"),
44 content: format!(""),
45 from: usize::from(self.ws.token()),
46 to: usize::from(connection.token()),
47 };
48
49 match serde_json::to_string(&connected_message) {
50 Ok(s) => {
51 let msg = Message::text(s);
52 connection.send(msg)?;
53 },
54 Err(e) => println!("{:?}", e)
55 };
56 }
57
58 connections.push(self.ws.clone());
59
60 Ok(())
61 }
62
63 fn on_message(&mut self, msg: Message) -> Result<()> {
64 println!("*********Server got message '{}'. ", msg);
65
66 match serde_json::from_str::<MessageJSON>(&msg.into_text()?) {
67 Ok(received_message) => {
68 if received_message.type_name == String::from("signal") {
69 let connections = self.connections.borrow();
70 for connection in connections.iter() {
71 if usize::from(connection.token()) == received_message.to {
72 let signal_message = MessageJSON {
73 type_name: String::from("signal"),
74 content: received_message.content.to_owned(),
75 from: usize::from(self.ws.token()),
76 to: usize::from(connection.token()),
77 };
78
79 match serde_json::to_string(&signal_message) {
80 Ok(s) => {
81 let msg = Message::text(s);
82 connection.send(msg);
83 },
84 Err(e) => println!("{:?}", e)
85 };
86 }
87 }
88 }
89 },
90 Err(e) => println!("{:?}", e)
91 };
92
93 Ok(())
94 }
95
96 fn on_close(&mut self, code: CloseCode, reason: &str) {
97 let mut connections = self.connections.borrow_mut();
98 let index = connections.iter().position(|i| i.token() == self.ws.token()).unwrap();
99 connections.remove(index);
100
101 for connection in connections.iter() {
102 let disconnected_message = MessageJSON {
103 type_name: String::from("disconnected"),
104 content: format!(""),
105 from: usize::from(self.ws.token()),
106 to: usize::from(connection.token()),
107 };
108
109 match serde_json::to_string(&disconnected_message) {
110 Ok(s) => {
111 let msg = Message::text(s);
112 connection.send(msg);
113 },
114 Err(e) => println!("{:?}", e)
115 };
116 }
117
118 match code {
119 CloseCode::Normal => println!("The client is done with the connection."),
120 CloseCode::Away => println!("The client is leaving the site."),
121 _ => println!("The client encountered an error: {}", reason),
122 }
123 }
124}
125
126pub struct MultiVP {
127 pub address: String,
128 pub port: u16,
129}
130
131impl MultiVP {
132 pub fn new(address: String, port: u16) -> MultiVP {
133 MultiVP {
134 address: address,
135 port: port
136 }
137 }
138
139 pub fn run_server(&self) {
140 let connections = Connections::new(RefCell::new(Vec::with_capacity(10_000)));;
141 listen(format!("{}:{}", self.address, self.port), move |out| {
142 Server {
143 ws: out,
144 connections: connections.clone()
145 }
146 }).unwrap()
147 }
148}