From 17175afd7311c55abd8deb490ef538d89e6aa8ca Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Thu, 16 Aug 2012 13:28:28 -0500 Subject: [PATCH] websock.js: make protocols parameter of open(). If no protocols are selected then defaults to ['binary', 'base64'] (or just 'base64' if there is not full binary type support. Checks to make sure binary types are fully supported and throws an exception if they are requested but not supported. --- include/websock.js | 47 +++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/include/websock.js b/include/websock.js index b58e9fd..6402eb2 100644 --- a/include/websock.js +++ b/include/websock.js @@ -61,7 +61,6 @@ function Websock() { var api = {}, // Public API websocket = null, // WebSocket object - protocols, // Protocols to request in priority order mode = 'base64', rQ = [], // Receive queue rQi = 0, // Receive queue index @@ -268,40 +267,62 @@ function on(evt, handler) { eventHandlers[evt] = handler; } -function init() { +function init(protocols) { rQ = []; rQi = 0; sQ = []; - websocket = null, - protocols = "base64"; + websocket = null; var bt = false, - wsbt = false; + wsbt = false, + try_binary = false; + // Check for full typed array support if (('Uint8Array' in window) && ('set' in Uint8Array.prototype)) { bt = true; } + + // Check for full binary type support in WebSockets // TODO: this sucks, the property should exist on the prototype // but it does not. try { if (bt && ('binaryType' in (new WebSocket("ws://localhost:17523")))) { + Util.Info("Detected binaryType support in WebSockets"); wsbt = true; } } catch (exc) { // Just ignore failed test localhost connections } - if (bt && wsbt) { - Util.Info("Detected binaryType support in WebSockets"); - protocols = ['binary', 'base64']; - } else { - Util.Info("No binaryType support in WebSockets, using base64 encoding"); - protocols = 'base64'; + + // Default protocols if not specified + if (typeof(protocols) === "undefined") { + if (wsbt) { + protocols = ['binary', 'base64']; + } else { + protocols = 'base64'; + } } + + // If no binary support, make sure it was not requested + if (!wsbt) { + if (protocols === 'binary') { + throw("WebSocket binary sub-protocol requested but not supported"); + } + if (typeof(protocols) === "object") { + for (var i = 0; i < protocols.length; i++) { + if (protocols[i] === 'binary') { + throw("WebSocket binary sub-protocol requested but not supported"); + } + } + } + } + + return protocols; } -function open(uri) { - init(); +function open(uri, protocols) { + protocols = init(protocols); if (test_mode) { websocket = {};