From 96890fab976097b790edbd32dcd981228554df7f Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Mon, 17 Sep 2012 16:49:36 -0500 Subject: [PATCH] include/websock.js: cleanup and fix protocol list check. If a protocol list is specified and we don't support binary WebSockets then strip binary from the list and check the list to make sure there is still an option left. --- include/websock.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/websock.js b/include/websock.js index 6402eb2..ccb7d4c 100644 --- a/include/websock.js +++ b/include/websock.js @@ -61,7 +61,7 @@ function Websock() { var api = {}, // Public API websocket = null, // WebSocket object - mode = 'base64', + mode = 'base64', // Current WebSocket mode: 'binary', 'base64' rQ = [], // Receive queue rQi = 0, // Receive queue index rQmax = 10000, // Max receive queue size before compacting @@ -168,10 +168,11 @@ function rQwait(msg, num, goback) { // function encode_message() { - /* base64 encode */ if (mode === 'binary') { + // Put in a binary arraybuffer return (new Uint8Array(sQ)).buffer; } else { + // base64 encode return Base64.encode(sQ); } } @@ -310,11 +311,19 @@ function init(protocols) { throw("WebSocket binary sub-protocol requested but not supported"); } if (typeof(protocols) === "object") { + var new_protocols = []; for (var i = 0; i < protocols.length; i++) { if (protocols[i] === 'binary') { - throw("WebSocket binary sub-protocol requested but not supported"); + Util.Error("Skipping unsupported WebSocket binary sub-protocol"); + } else { + new_protocols.push(protocols[i]); } } + if (new_protocols.length > 0) { + protocols = new_protocols; + } else { + throw("Only WebSocket binary sub-protocol was requested and not supported."); + } } }