diff options
author | Hiroshi Ichikawa <gimite@gmail.com> | 2010-06-27 14:18:12 +0900 |
---|---|---|
committer | Hiroshi Ichikawa <gimite@gmail.com> | 2010-06-27 14:18:12 +0900 |
commit | 6cf9eba2e59323ac0f9cc8bce384f52abc877905 (patch) | |
tree | c5faaa74634b99455fda2737e5640e83503c505c | |
parent | bd2ea43b96f9f0de446fbd544c053a401aedc4dd (diff) | |
download | web-socket-js-6cf9eba2e59323ac0f9cc8bce384f52abc877905.tar.gz |
Using WebSocket.__debug to switch debug mode instead of argument to WebSocket.__initialize().
Reenabling proxy support.
Adding option not to call WebSocket.__initialize() automatically (by WebSocket.__disableAutoInitialization).
-rw-r--r-- | README.txt | 5 | ||||
-rw-r--r-- | WebSocketMain.swf | bin | 179159 -> 179849 bytes | |||
-rw-r--r-- | flash-src/WebSocket.as | 55 | ||||
-rw-r--r-- | flash-src/WebSocketMain.as | 2 | ||||
-rwxr-xr-x | flash-src/build.sh | 6 | ||||
-rw-r--r-- | sample.html | 2 | ||||
-rwxr-xr-x | web_socket.js | 16 |
7 files changed, 46 insertions, 40 deletions
@@ -67,10 +67,11 @@ The class RFC2817Socket (by Christian Cantrell) effectively lets us implement th * How to build WebSocketMain.swf -Install Flex SDK. +Install Flex 4 SDK: +http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4 $ cd flash-src -$ mxmlc -output=../WebSocketMain.swf WebSocketMain.as +$ ./build.sh * License diff --git a/WebSocketMain.swf b/WebSocketMain.swf Binary files differindex 80b074e..031b700 100644 --- a/WebSocketMain.swf +++ b/WebSocketMain.swf diff --git a/flash-src/WebSocket.as b/flash-src/WebSocket.as index 43996e1..1f20c00 100644 --- a/flash-src/WebSocket.as +++ b/flash-src/WebSocket.as @@ -1,7 +1,7 @@ // Copyright: Hiroshi Ichikawa <http://gimite.net/en/> // License: New BSD License // Reference: http://dev.w3.org/html5/websockets/ -// Reference: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-31 +// Reference: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 package { @@ -34,7 +34,6 @@ public class WebSocket extends EventDispatcher { private static var CLOSING:int = 2; private static var CLOSED:int = 3; - //private var rawSocket:RFC2817Socket; private var rawSocket:Socket; private var tlsSocket:TLSSocket; private var tlsConfig:TLSConfig; @@ -75,51 +74,49 @@ public class WebSocket extends EventDispatcher { // "Header1: xxx\r\nHeader2: yyyy\r\n" this.headers = headers; - /* - socket = new RFC2817Socket(); - - // if no proxy information is supplied, it acts like a normal Socket - // @see RFC2817Socket::connect - if (proxyHost != null && proxyPort != 0){ - socket.setProxyInfo(proxyHost, proxyPort); - } - */ - - ExternalInterface.call("console.log", "[WebSocket] scheme: " + scheme); - rawSocket = new Socket(); - - rawSocket.addEventListener(Event.CLOSE, onSocketClose); - rawSocket.addEventListener(Event.CONNECT, onSocketConnect); - rawSocket.addEventListener(IOErrorEvent.IO_ERROR, onSocketIoError); - rawSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSocketSecurityError); - if (scheme == "wss") { + if (proxyHost != null && proxyPort != 0){ + if (scheme == "wss") { + main.fatal("wss with proxy is not supported"); + } + var proxySocket:RFC2817Socket = new RFC2817Socket(); + proxySocket.setProxyInfo(proxyHost, proxyPort); + proxySocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); + rawSocket = socket = proxySocket; + } else { + rawSocket = new Socket(); + if (scheme == "wss") { tlsConfig= new TLSConfig(TLSEngine.CLIENT, null, null, null, null, null, TLSSecurityParameters.PROTOCOL_VERSION); tlsConfig.trustSelfSignedCertificates = true; tlsConfig.ignoreCommonNameMismatch = true; - tlsSocket = new TLSSocket(); tlsSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); - socket = (tlsSocket as Socket); - } else { + socket = tlsSocket; + } else { rawSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); - socket = (rawSocket as Socket); + socket = rawSocket; + } } + rawSocket.addEventListener(Event.CLOSE, onSocketClose); + rawSocket.addEventListener(Event.CONNECT, onSocketConnect); + rawSocket.addEventListener(IOErrorEvent.IO_ERROR, onSocketIoError); + rawSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSocketSecurityError); rawSocket.connect(host, port); } - public function send(data:String):int { + public function send(encData:String):int { + var data:String = decodeURIComponent(encData); if (readyState == OPEN) { socket.writeByte(0x00); - socket.writeUTFBytes(decodeURIComponent(data)); + socket.writeUTFBytes(data); socket.writeByte(0xff); socket.flush(); main.log("sent: " + data); return -1; } else if (readyState == CLOSED) { var bytes:ByteArray = new ByteArray(); - bytes.writeUTFBytes(decodeURIComponent(data)); + bytes.writeUTFBytes(data); bufferedAmount += bytes.length; // not sure whether it should include \x00 and \xff // We use return value to let caller know bufferedAmount because we cannot fire // stateChange event here which causes weird error: @@ -154,8 +151,8 @@ public class WebSocket extends EventDispatcher { main.log("connected"); if (scheme == "wss") { - ExternalInterface.call("console.log", "[WebSocket] starting SSL/TLS"); - tlsSocket.startTLS(rawSocket, host, tlsConfig); + main.log("starting SSL/TLS"); + tlsSocket.startTLS(rawSocket, host, tlsConfig); } var hostValue:String = host + (port == 80 ? "" : ":" + port); diff --git a/flash-src/WebSocketMain.as b/flash-src/WebSocketMain.as index 140241c..41991e7 100644 --- a/flash-src/WebSocketMain.as +++ b/flash-src/WebSocketMain.as @@ -76,7 +76,7 @@ public class WebSocketMain extends Sprite { public function log(message:String):void { if (debug) { - ExternalInterface.call("webSocketLog", encodeURIComponent("[WebSocket] " + message)); + ExternalInterface.call("webSocketLog", encodeURIComponent("[WebSocket] " + message)); } } diff --git a/flash-src/build.sh b/flash-src/build.sh new file mode 100755 index 0000000..d7b7577 --- /dev/null +++ b/flash-src/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# You need Flex 4 SDK: +# http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4 + +mxmlc -static-link-runtime-shared-libraries -output=../WebSocketMain.swf WebSocketMain.as diff --git a/sample.html b/sample.html index 4dbdbe8..ac9ca01 100644 --- a/sample.html +++ b/sample.html @@ -15,6 +15,8 @@ // Set URL of your WebSocketMain.swf here: WebSocket.__swfLocation = "WebSocketMain.swf"; + // Set this to dump debug message from Flash to console.log: + WebSocket.__debug = true; var ws; diff --git a/web_socket.js b/web_socket.js index 79d8b4f..481d4bd 100755 --- a/web_socket.js +++ b/web_socket.js @@ -255,7 +255,7 @@ WebSocket.__tasks = []; - WebSocket.__initialize = function(debug) { + WebSocket.__initialize = function() { if (!WebSocket.__swfLocation) { console.error("[WebSocket] set WebSocket.__swfLocation to location of WebSocketMain.swf"); return; @@ -283,9 +283,7 @@ //console.log("[WebSocket] FABridge initializad"); WebSocket.__flash = FABridge.webSocket.root(); WebSocket.__flash.setCallerUrl(location.href); - if (typeof debug !== "undefined") { - WebSocket.__flash.setDebug(debug); - } + WebSocket.__flash.setDebug(!!WebSocket.__debug); for (var i = 0; i < WebSocket.__tasks.length; ++i) { WebSocket.__tasks[i](); } @@ -314,10 +312,12 @@ console.error(decodeURIComponent(message)); }; - if (window.addEventListener) { - window.addEventListener("load", WebSocket.__initialize, false); - } else { - window.attachEvent("onload", WebSocket.__initialize); + if (!WebSocket.__disableAutoInitialization) { + if (window.addEventListener) { + window.addEventListener("load", WebSocket.__initialize, false); + } else { + window.attachEvent("onload", WebSocket.__initialize); + } } })(); |