Skip to content

Commit

Permalink
Merge branch 'master' of http://github.com/gimite/web-socket-js
Browse files Browse the repository at this point in the history
Conflicts:
	WebSocketMain.swf
  • Loading branch information
Vagabond committed Sep 23, 2010
2 parents 6c6981c + 8e46ce3 commit 86ad23e
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion FABridge.js
Expand Up @@ -362,7 +362,7 @@ FABridge.prototype =
// accepts a type structure, returns a constructed type
addTypeDataToCache: function(typeData)
{
newType = new ASProxy(this, typeData.name);
var newType = new ASProxy(this, typeData.name);
var accessors = typeData.accessors;
for (var i = 0; i < accessors.length; i++)
{
Expand Down
11 changes: 11 additions & 0 deletions README.txt
Expand Up @@ -84,6 +84,17 @@ The AS3 Socket class doesn't implement this mechanism, which renders it useless
The class RFC2817Socket (by Christian Cantrell) effectively lets us implement this, as long as the proxy settings are known and provided by the interface that instantiates the WebSocket. As such, if you want to support proxied conncetions, you'll have to supply this information to the WebSocket constructor when Flash is being used. One way to go about it would be to ask the user for proxy settings information if the initial connection fails.


* How to host HTML file and SWF file in different domains

By default, HTML file and SWF file must be in the same domain. You can follow steps below to allow hosting them in different domain.

WARNING: If you use the method below, HTML files in ANY domains can send arbitrary TCP data to your WebSocket server, regardless of configuration in Flash socket policy file. Arbitrary TCP data means that they can even fake request headers including Origin and Cookie.

- Unzip WebSocketMainInsecure.zip to extract WebSocketMainInsecure.swf.
- Put WebSocketMainInsecure.swf on your server, instead of WebSocketMain.swf.
- In JavaScript, set WEB_SOCKET_SWF_LOCATION to URL of your WebSocketMainInsecure.swf.


* How to build WebSocketMain.swf

Install Flex 4 SDK:
Expand Down
Binary file modified WebSocketMain.swf
Binary file not shown.
Binary file added WebSocketMainInsecure.zip
Binary file not shown.
8 changes: 5 additions & 3 deletions flash-src/WebSocket.as
Expand Up @@ -138,9 +138,11 @@ public class WebSocket extends EventDispatcher {
main.log("close");
dataQueue = [];
try {
socket.writeByte(0xff);
socket.writeByte(0x00);
socket.flush();
if (readyState == OPEN) {
socket.writeByte(0xff);
socket.writeByte(0x00);
socket.flush();
}
socket.close();
} catch (ex:Error) { }
readyState = CLOSED;
Expand Down
2 changes: 1 addition & 1 deletion flash-src/WebSocketMain.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 {

Expand Down
19 changes: 19 additions & 0 deletions flash-src/WebSocketMainInsecure.as
@@ -0,0 +1,19 @@
// 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-76

package {

import flash.system.*;

public class WebSocketMainInsecure extends WebSocketMain {

public function WebSocketMainInsecure() {
Security.allowDomain("*");
super();
}

}

}
6 changes: 5 additions & 1 deletion flash-src/build.sh
Expand Up @@ -3,4 +3,8 @@
# 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
mxmlc -static-link-runtime-shared-libraries -output=../WebSocketMain.swf WebSocketMain.as &&
mxmlc -static-link-runtime-shared-libraries -output=../WebSocketMainInsecure.swf WebSocketMainInsecure.as &&
cd .. &&
zip WebSocketMainInsecure.zip WebSocketMainInsecure.swf &&
rm WebSocketMainInsecure.swf
4 changes: 3 additions & 1 deletion sample.html
Expand Up @@ -17,7 +17,9 @@
WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";
// Set this to dump debug message from Flash to console.log:
WEB_SOCKET_DEBUG = true;


// Everything below is the same as using standard WebSocket.

var ws;

function init() {
Expand Down
21 changes: 13 additions & 8 deletions web_socket.js
Expand Up @@ -117,16 +117,21 @@
};

WebSocket.prototype.close = function() {
if (!this.__flash) return;
this.readyState = this.__flash.getReadyState();
if (this.readyState != WebSocket.OPEN) return;
this.__flash.close();
var self = this;
if (!self.__flash) return;
self.readyState = self.__flash.getReadyState();
if (self.readyState == WebSocket.CLOSED || self.readyState == WebSocket.CLOSING) return;
self.__flash.close();
// Sets/calls them manually here because Flash WebSocketConnection.close cannot fire events
// which causes weird error:
// > You are trying to call recursively into the Flash Player which is not allowed.
this.readyState = WebSocket.CLOSED;
if (this.__timer) clearInterval(this.__timer);
if (this.onclose) this.onclose();
self.readyState = WebSocket.CLOSED;
if (self.__timer) clearInterval(self.__timer);
if (self.onclose) {
// Make it asynchronous so that it looks more like an actual
// close event
setTimeout(self.onclose, 1);
}
};

/**
Expand Down Expand Up @@ -326,7 +331,7 @@
swfobject.embedSWF(
WEB_SOCKET_SWF_LOCATION, "webSocketFlash",
"1" /* width */, "1" /* height */, "9.0.0" /* SWF version */,
null, {bridgeName: "webSocket"}, {hasPriority: true}, null,
null, {bridgeName: "webSocket"}, {hasPriority: true, allowScriptAccess: "always"}, null,
function(e) {
if (!e.success) console.error("[WebSocket] swfobject.embedSWF failed");
}
Expand Down

0 comments on commit 86ad23e

Please sign in to comment.