pub struct WebSocket { /* private fields */ }Implementations§
Source§impl WebSocket
impl WebSocket
pub fn accept(stream: TcpStream, key: &str) -> Option<Self>
Sourcepub fn read_message(&mut self) -> Option<Message>
pub fn read_message(&mut self) -> Option<Message>
Examples found in repository?
examples/web_app.rs (line 100)
95fn ws_handler(mut ws: WebSocket) {
96 let welcome = r#"{"type":"welcome","message":"Connected to WebSocket server"}"#;
97 ws.send_text(welcome);
98
99 loop {
100 match ws.read_message() {
101 Some(Message::Text(text)) => {
102 let reply = format!(r#"{{"type":"echo","data":"{}"}}"#, text);
103 ws.send_text(&reply);
104 }
105 Some(Message::Binary(data)) => {
106 ws.send_binary(&data);
107 }
108 Some(Message::Ping(data)) => {
109 ws.send_pong(&data);
110 }
111 Some(Message::Close(_)) => {
112 break;
113 }
114 _ => break,
115 }
116 }
117}Sourcepub fn send_text(&mut self, text: &str) -> bool
pub fn send_text(&mut self, text: &str) -> bool
Examples found in repository?
examples/web_app.rs (line 97)
95fn ws_handler(mut ws: WebSocket) {
96 let welcome = r#"{"type":"welcome","message":"Connected to WebSocket server"}"#;
97 ws.send_text(welcome);
98
99 loop {
100 match ws.read_message() {
101 Some(Message::Text(text)) => {
102 let reply = format!(r#"{{"type":"echo","data":"{}"}}"#, text);
103 ws.send_text(&reply);
104 }
105 Some(Message::Binary(data)) => {
106 ws.send_binary(&data);
107 }
108 Some(Message::Ping(data)) => {
109 ws.send_pong(&data);
110 }
111 Some(Message::Close(_)) => {
112 break;
113 }
114 _ => break,
115 }
116 }
117}Sourcepub fn send_binary(&mut self, data: &[u8]) -> bool
pub fn send_binary(&mut self, data: &[u8]) -> bool
Examples found in repository?
examples/web_app.rs (line 106)
95fn ws_handler(mut ws: WebSocket) {
96 let welcome = r#"{"type":"welcome","message":"Connected to WebSocket server"}"#;
97 ws.send_text(welcome);
98
99 loop {
100 match ws.read_message() {
101 Some(Message::Text(text)) => {
102 let reply = format!(r#"{{"type":"echo","data":"{}"}}"#, text);
103 ws.send_text(&reply);
104 }
105 Some(Message::Binary(data)) => {
106 ws.send_binary(&data);
107 }
108 Some(Message::Ping(data)) => {
109 ws.send_pong(&data);
110 }
111 Some(Message::Close(_)) => {
112 break;
113 }
114 _ => break,
115 }
116 }
117}pub fn send_ping(&mut self, data: &[u8]) -> bool
Sourcepub fn send_pong(&mut self, data: &[u8]) -> bool
pub fn send_pong(&mut self, data: &[u8]) -> bool
Examples found in repository?
examples/web_app.rs (line 109)
95fn ws_handler(mut ws: WebSocket) {
96 let welcome = r#"{"type":"welcome","message":"Connected to WebSocket server"}"#;
97 ws.send_text(welcome);
98
99 loop {
100 match ws.read_message() {
101 Some(Message::Text(text)) => {
102 let reply = format!(r#"{{"type":"echo","data":"{}"}}"#, text);
103 ws.send_text(&reply);
104 }
105 Some(Message::Binary(data)) => {
106 ws.send_binary(&data);
107 }
108 Some(Message::Ping(data)) => {
109 ws.send_pong(&data);
110 }
111 Some(Message::Close(_)) => {
112 break;
113 }
114 _ => break,
115 }
116 }
117}pub fn send_close(&mut self, code: u16, reason: &str) -> bool
Auto Trait Implementations§
impl Freeze for WebSocket
impl RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin for WebSocket
impl UnsafeUnpin for WebSocket
impl UnwindSafe for WebSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more