diff options
author | Hiroshi Ichikawa <gimite@gmail.com> | 2011-05-06 14:53:44 +0900 |
---|---|---|
committer | Hiroshi Ichikawa <gimite@gmail.com> | 2011-05-06 14:53:44 +0900 |
commit | 3cc9cdc9cf62bd3e2b60b375c6ac5b7a008956dd (patch) | |
tree | 6cd43f6a7665d737f65324b7bea72f2f95287a73 | |
parent | c1bde783368d575d789219dc40cddf3671307ae7 (diff) | |
download | web-socket-js-3cc9cdc9cf62bd3e2b60b375c6ac5b7a008956dd.tar.gz |
Accepting array in addition to string for protocol parameter.
-rw-r--r-- | WebSocketMain.swf | bin | 175753 -> 175830 bytes | |||
-rw-r--r-- | WebSocketMainInsecure.zip | bin | 166462 -> 166610 bytes | |||
-rw-r--r-- | flash-src/WebSocket.as | 24 | ||||
-rw-r--r-- | flash-src/WebSocketMain.as | 6 | ||||
-rw-r--r-- | web_socket.js | 11 |
5 files changed, 24 insertions, 17 deletions
diff --git a/WebSocketMain.swf b/WebSocketMain.swf Binary files differindex 0fb5449..20a451f 100644 --- a/WebSocketMain.swf +++ b/WebSocketMain.swf diff --git a/WebSocketMainInsecure.zip b/WebSocketMainInsecure.zip Binary files differindex 691f84c..58e2a4e 100644 --- a/WebSocketMainInsecure.zip +++ b/WebSocketMainInsecure.zip diff --git a/flash-src/WebSocket.as b/flash-src/WebSocket.as index 5373b90..813f517 100644 --- a/flash-src/WebSocket.as +++ b/flash-src/WebSocket.as @@ -42,7 +42,8 @@ public class WebSocket extends EventDispatcher { private var port:uint; private var path:String; private var origin:String; - private var protocol:String; + private var requestedProtocols:Array; + private var acceptedProtocol:String; private var buffer:ByteArray = new ByteArray(); private var headerState:int = 0; private var readyState:int = CONNECTING; @@ -53,7 +54,7 @@ public class WebSocket extends EventDispatcher { private var logger:IWebSocketLogger; public function WebSocket( - id:int, url:String, protocol:String, origin:String, + id:int, url:String, protocols:Array, origin:String, proxyHost:String, proxyPort:int, cookie:String, headers:String, logger:IWebSocketLogger) { @@ -69,7 +70,7 @@ public class WebSocket extends EventDispatcher { this.port = parseInt(m[4]) || defaultPort; this.path = (m[5] || "/") + (m[6] || ""); this.origin = origin; - this.protocol = protocol; + this.requestedProtocols = protocols; this.cookie = cookie; // if present and not the empty string, headers MUST end with \r\n // headers should be zero or more complete lines, for example @@ -121,8 +122,8 @@ public class WebSocket extends EventDispatcher { return this.readyState; } - public function getProtocol():String { - return this.protocol; + public function getAcceptedProtocol():String { + return this.acceptedProtocol; } public function send(encData:String):int { @@ -173,7 +174,9 @@ public class WebSocket extends EventDispatcher { var key3:String = generateKey3(); expectedDigest = getSecurityDigest(key1, key2, key3); var opt:String = ""; - if (protocol) opt += "Sec-WebSocket-Protocol: " + protocol + "\r\n"; + if (requestedProtocols.length > 0) { + opt += "Sec-WebSocket-Protocol: " + requestedProtocols.join(",") + "\r\n"; + } // if caller passes additional headers they must end with "\r\n" if (headers) opt += headers; @@ -328,12 +331,11 @@ public class WebSocket extends EventDispatcher { onError("origin doesn't match: '" + resOrigin + "' != '" + origin + "'"); return false; } - if (protocol) { - if (protocol.split(",").indexOf(header["sec-websocket-protocol"]) >= 0) { - protocol = header["sec-websocket-protocol"]; - } else { + if (requestedProtocols.length > 0) { + acceptedProtocol = header["sec-websocket-protocol"]; + if (requestedProtocols.indexOf(acceptedProtocol) < 0) { onError("protocol doesn't match: '" + - header["websocket-protocol"] + "' not in '" + protocol + "'"); + acceptedProtocol + "' not in '" + requestedProtocols.join(",") + "'"); return false; } } diff --git a/flash-src/WebSocketMain.as b/flash-src/WebSocketMain.as index 12d3329..1bf3d7e 100644 --- a/flash-src/WebSocketMain.as +++ b/flash-src/WebSocketMain.as @@ -70,7 +70,7 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{ eventObj.type = event.type; eventObj.webSocketId = webSocket.getId(); eventObj.readyState = webSocket.getReadyState(); - eventObj.protocol = webSocket.getProtocol(); + eventObj.protocol = webSocket.getAcceptedProtocol(); if (event.message !== null) { eventObj.message = event.message; } @@ -79,14 +79,14 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{ public function create( webSocketId:int, - url:String, protocol:String, + url:String, protocols:Array, proxyHost:String = null, proxyPort:int = 0, headers:String = null):void { if (!manualPolicyFileLoaded) { loadDefaultPolicyFile(url); } var newSocket:WebSocket = new WebSocket( - webSocketId, url, protocol, getOrigin(), proxyHost, proxyPort, + webSocketId, url, protocols, getOrigin(), proxyHost, proxyPort, getCookie(url), headers, this); newSocket.addEventListener("open", onSocketEvent); newSocket.addEventListener("close", onSocketEvent); diff --git a/web_socket.js b/web_socket.js index d080c6e..bed899c 100644 --- a/web_socket.js +++ b/web_socket.js @@ -26,24 +26,29 @@ /** * This class represents a faux web socket. * @param {string} url - * @param {string} protocol + * @param {array or string} protocols * @param {string} proxyHost * @param {int} proxyPort * @param {string} headers */ - WebSocket = function(url, protocol, proxyHost, proxyPort, headers) { + WebSocket = function(url, protocols, proxyHost, proxyPort, headers) { var self = this; self.__id = WebSocket.__nextId++; WebSocket.__instances[self.__id] = self; self.readyState = WebSocket.CONNECTING; self.bufferedAmount = 0; self.__events = {}; + if (!protocols) { + protocols = []; + } else if (typeof protocols == "string") { + protocols = [protocols]; + } // Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc. // Otherwise, when onopen fires immediately, onopen is called before it is set. setTimeout(function() { WebSocket.__addTask(function() { WebSocket.__flash.create( - self.__id, url, protocol, proxyHost || null, proxyPort || 0, headers || null); + self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null); }); }, 0); }; |