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
use message::Message; use event::Event; use std::sync::mpsc::Sender; use serde_json; use websocket::OwnedMessage; use serde_json::Value; pub struct Channel { topic: String, tx: Sender<OwnedMessage>, reference: String, } impl Channel { pub fn new(topic: &str, tx: Sender<OwnedMessage>, reference: &str) -> Channel { Channel{ topic: topic.to_owned(), tx: tx, reference: reference.to_owned(), } } pub fn send(&mut self, event: &str, msg: Value) { let msg = Message{ topic: self.topic.to_owned(), event: event.to_string(), reference: Some(self.reference.to_owned()), join_ref: Some(self.reference.to_owned()), payload: msg.to_owned(), }; self.tx.send(OwnedMessage::Text(serde_json::to_string(&msg).unwrap())).unwrap(); } pub fn join(&mut self) { let msg = Message{ topic: self.topic.to_owned(), event: Event::Join.to_string(), reference: Some(self.reference.to_owned()), join_ref: Some(self.reference.to_owned()), payload: Value::Null, }; self.tx.send(OwnedMessage::Text(serde_json::to_string(&msg).unwrap())).unwrap(); } }