Merge pull request #37 from bluetech/wsstream-without-handshake

Allow constructing WebSocketStream without performing a handshake
pull/1/head
Daniel Abramov 7 years ago committed by GitHub
commit 6ac55ac555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      src/lib.rs

@ -34,7 +34,7 @@ use tokio_io::{AsyncRead, AsyncWrite};
use tungstenite::handshake::client::{ClientHandshake, Response, Request}; use tungstenite::handshake::client::{ClientHandshake, Response, Request};
use tungstenite::handshake::server::{ServerHandshake, Callback, NoCallback}; use tungstenite::handshake::server::{ServerHandshake, Callback, NoCallback};
use tungstenite::handshake::{HandshakeRole, HandshakeError}; use tungstenite::handshake::{HandshakeRole, HandshakeError};
use tungstenite::protocol::{WebSocket, Message}; use tungstenite::protocol::{WebSocket, Message, Role};
use tungstenite::error::Error as WsError; use tungstenite::error::Error as WsError;
use tungstenite::server; use tungstenite::server;
@ -113,6 +113,22 @@ pub struct WebSocketStream<S> {
inner: WebSocket<S>, inner: WebSocket<S>,
} }
impl<S> WebSocketStream<S> {
/// Convert a raw socket into a WebSocketStream without performing a
/// handshake.
pub fn from_raw_socket(stream: S, role: Role) -> Self {
let ws = WebSocket::from_raw_socket(stream, role);
WebSocketStream { inner: ws }
}
/// Convert a raw socket into a WebSocketStream without performing a
/// handshake.
pub fn from_partially_read(stream: S, part: Vec<u8>, role: Role) -> Self {
let ws = WebSocket::from_partially_read(stream, part, role);
WebSocketStream { inner: ws }
}
}
impl<T> Stream for WebSocketStream<T> where T: AsyncRead + AsyncWrite { impl<T> Stream for WebSocketStream<T> where T: AsyncRead + AsyncWrite {
type Item = Message; type Item = Message;
type Error = WsError; type Error = WsError;

Loading…
Cancel
Save