diff options
author | Hiroshi Ichikawa <gimite@gmail.com> | 2011-05-07 11:01:51 +0900 |
---|---|---|
committer | Hiroshi Ichikawa <gimite@gmail.com> | 2011-05-07 11:01:51 +0900 |
commit | fd5fa42d4428b766c28b87c64c8c67162974b9f0 (patch) | |
tree | ac76139a569356dc39b96f806955e13e9daa0d3f | |
parent | 9e67e24ba3af73fda6fd7adabe95d37ef8ca50e2 (diff) | |
parent | 3cc9cdc9cf62bd3e2b60b375c6ac5b7a008956dd (diff) | |
download | web-socket-js-fd5fa42d4428b766c28b87c64c8c67162974b9f0.tar.gz |
Merging.
-rw-r--r-- | WebSocketMain.swf | bin | 175506 -> 175519 bytes | |||
-rw-r--r-- | WebSocketMainInsecure.zip | bin | 166162 -> 166111 bytes | |||
-rw-r--r-- | flash-src/WebSocket.as | 33 | ||||
-rw-r--r-- | flash-src/WebSocketMain.as | 6 | ||||
-rw-r--r-- | web_socket.js | 11 |
5 files changed, 28 insertions, 22 deletions
diff --git a/WebSocketMain.swf b/WebSocketMain.swf Binary files differindex 7fa6666..073d923 100644 --- a/WebSocketMain.swf +++ b/WebSocketMain.swf diff --git a/WebSocketMainInsecure.zip b/WebSocketMainInsecure.zip Binary files differindex d83c898..c0ce704 100644 --- a/WebSocketMainInsecure.zip +++ b/WebSocketMainInsecure.zip diff --git a/flash-src/WebSocket.as b/flash-src/WebSocket.as index d2bdf11..63ee5af 100644 --- a/flash-src/WebSocket.as +++ b/flash-src/WebSocket.as @@ -44,7 +44,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; @@ -60,7 +61,7 @@ public class WebSocket extends EventDispatcher { private var frame_plength:uint = 0; 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) { @@ -75,7 +76,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 @@ -127,8 +128,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 { @@ -259,7 +260,9 @@ public class WebSocket extends EventDispatcher { expectedDigest = SHA1.b64_sha1(key + GUID); 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; @@ -387,21 +390,19 @@ public class WebSocket extends EventDispatcher { "Try newer version of the server if available."); return false; } - if (protocol) { - if (protocol.split(",").indexOf(header["sec-websocket-protocol"]) >= 0) { - protocol = header["sec-websocket-protocol"]; - } else { - onError("protocol doesn't match: '" + - header["websocket-protocol"] + "' not in '" + protocol + "'"); - return false; - } - } - var replyDigest:String = header["sec-websocket-accept"] if (replyDigest != expectedDigest) { onError("digest doesn't match: " + replyDigest + " != " + expectedDigest); return false; } + if (requestedProtocols.length > 0) { + acceptedProtocol = header["sec-websocket-protocol"]; + if (requestedProtocols.indexOf(acceptedProtocol) < 0) { + onError("protocol doesn't match: '" + + acceptedProtocol + "' not in '" + requestedProtocols.join(",") + "'"); + return false; + } + } return true; } diff --git a/flash-src/WebSocketMain.as b/flash-src/WebSocketMain.as index dd947d4..10fb3af 100644 --- a/flash-src/WebSocketMain.as +++ b/flash-src/WebSocketMain.as @@ -73,7 +73,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; } @@ -82,14 +82,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 5f359d3..1cf0249 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); }; |