From 03ca408e9422946d4863d9d70b82e698e73d8cda Mon Sep 17 00:00:00 2001 From: jairinhohw Date: Sat, 20 Jul 2019 02:22:06 -0300 Subject: [PATCH] add support for specifying protocols on websocket handshake (#835) * add support for specifying protocols on websocket handshake * separated the handshake function with and without protocols changed protocols type from Vec<&str> to [&str] --- actix-web-actors/src/ws.rs | 139 +++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 6 deletions(-) diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index ac08e396a..e25a7e6e4 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -60,15 +60,43 @@ where Ok((addr, res.streaming(out_stream))) } +/// Do websocket handshake and start ws actor. +/// +/// `protocols` is a sequence of known protocols. +pub fn start_with_protocols( + actor: A, + protocols: &[&str], + req: &HttpRequest, + stream: T, +) -> Result +where + A: Actor> + StreamHandler, + T: Stream + 'static, +{ + let mut res = handshake_with_protocols(req, protocols)?; + Ok(res.streaming(WebsocketContext::create(actor, stream))) +} + +/// Prepare `WebSocket` handshake response. +/// +/// This function returns handshake `HttpResponse`, ready to send to peer. +/// It does not perform any IO. +pub fn handshake(req: &HttpRequest) -> Result { + handshake_with_protocols(req, &[]) +} + /// Prepare `WebSocket` handshake response. /// /// This function returns handshake `HttpResponse`, ready to send to peer. /// It does not perform any IO. /// -// /// `protocols` is a sequence of known protocols. On successful handshake, -// /// the returned response headers contain the first protocol in this list -// /// which the server also knows. -pub fn handshake(req: &HttpRequest) -> Result { +/// `protocols` is a sequence of known protocols. On successful handshake, +/// the returned response headers contain the first protocol in this list +/// which the server also knows. +pub fn handshake_with_protocols( + req: &HttpRequest, + protocols: &[&str], +) -> Result { // WebSocket accepts only GET if *req.method() != Method::GET { return Err(HandshakeError::GetMethodRequired); @@ -117,11 +145,28 @@ pub fn handshake(req: &HttpRequest) -> Result