Skip to main content

WebSocket

Struct WebSocket 

Source
pub struct WebSocket { /* private fields */ }

Implementations§

Source§

impl WebSocket

Source

pub fn accept(stream: TcpStream, key: &str) -> Option<Self>

Source

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}
Source

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}
Source

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}
Source

pub fn send_ping(&mut self, data: &[u8]) -> bool

Source

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}
Source

pub fn send_close(&mut self, code: u16, reason: &str) -> bool

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.