|
|
@ -40,10 +40,7 @@ pub struct WebSocket<Stream> { |
|
|
|
pong: Option<Frame>, |
|
|
|
pong: Option<Frame>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<Stream> WebSocket<Stream> |
|
|
|
impl<Stream> WebSocket<Stream> { |
|
|
|
where Stream: Read + Write |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Convert a raw socket into a WebSocket without performing a handshake.
|
|
|
|
/// Convert a raw socket into a WebSocket without performing a handshake.
|
|
|
|
pub fn from_raw_socket(stream: Stream, role: Role) -> Self { |
|
|
|
pub fn from_raw_socket(stream: Stream, role: Role) -> Self { |
|
|
|
WebSocket::from_frame_socket(FrameSocket::new(stream), role) |
|
|
|
WebSocket::from_frame_socket(FrameSocket::new(stream), role) |
|
|
@ -54,6 +51,20 @@ impl<Stream> WebSocket<Stream> |
|
|
|
WebSocket::from_frame_socket(FrameSocket::from_partially_read(stream, part), role) |
|
|
|
WebSocket::from_frame_socket(FrameSocket::from_partially_read(stream, part), role) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Convert a frame socket into a WebSocket.
|
|
|
|
|
|
|
|
fn from_frame_socket(socket: FrameSocket<Stream>, role: Role) -> Self { |
|
|
|
|
|
|
|
WebSocket { |
|
|
|
|
|
|
|
role: role, |
|
|
|
|
|
|
|
socket: socket, |
|
|
|
|
|
|
|
state: WebSocketState::Active, |
|
|
|
|
|
|
|
incomplete: None, |
|
|
|
|
|
|
|
send_queue: VecDeque::new(), |
|
|
|
|
|
|
|
pong: None, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<Stream: Read + Write> WebSocket<Stream> { |
|
|
|
/// Read a message from stream, if possible.
|
|
|
|
/// Read a message from stream, if possible.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This function sends pong and close responses automatically.
|
|
|
|
/// This function sends pong and close responses automatically.
|
|
|
@ -141,18 +152,6 @@ impl<Stream> WebSocket<Stream> |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Convert a frame socket into a WebSocket.
|
|
|
|
|
|
|
|
fn from_frame_socket(socket: FrameSocket<Stream>, role: Role) -> Self { |
|
|
|
|
|
|
|
WebSocket { |
|
|
|
|
|
|
|
role: role, |
|
|
|
|
|
|
|
socket: socket, |
|
|
|
|
|
|
|
state: WebSocketState::Active, |
|
|
|
|
|
|
|
incomplete: None, |
|
|
|
|
|
|
|
send_queue: VecDeque::new(), |
|
|
|
|
|
|
|
pong: None, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Try to decode one message frame. May return None.
|
|
|
|
/// Try to decode one message frame. May return None.
|
|
|
|
fn read_message_frame(&mut self) -> Result<Option<Message>> { |
|
|
|
fn read_message_frame(&mut self) -> Result<Option<Message>> { |
|
|
|
if let Some(mut frame) = self.socket.read_frame()? { |
|
|
|
if let Some(mut frame) = self.socket.read_frame()? { |
|
|
|