Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rename JS WebSocket class to FlashWebSocket

  • Loading branch information...
commit 2ea12f6c0cc3e2cf7cfde70352f6ca2c82a0e87b 1 parent 9f7db36
@pl pl authored
View
BIN  WebSocketMain.swf
Binary file not shown
View
BIN  WebSocketMainInsecure.zip
Binary file not shown
View
44 flash-src/src/net/gimite/websocket/WebSocketMain.as
@@ -14,13 +14,13 @@ import mx.utils.URLUtil;
* Provides JavaScript API of WebSocket.
*/
public class WebSocketMain extends Sprite implements IWebSocketLogger{
-
+
private var callerUrl:String;
private var debug:Boolean = false;
private var manualPolicyFileLoaded:Boolean = false;
private var webSockets:Array = [];
private var eventQueue:Array = [];
-
+
public function WebSocketMain() {
ExternalInterface.addCallback("setCallerUrl", setCallerUrl);
ExternalInterface.addCallback("setDebug", setDebug);
@@ -29,42 +29,42 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
ExternalInterface.addCallback("close", close);
ExternalInterface.addCallback("loadManualPolicyFile", loadManualPolicyFile);
ExternalInterface.addCallback("receiveEvents", receiveEvents);
- ExternalInterface.call("WebSocket.__onFlashInitialized");
+ ExternalInterface.call("FlashWebSocket.__onFlashInitialized");
}
-
+
public function setCallerUrl(url:String):void {
callerUrl = url;
}
-
+
public function setDebug(val:Boolean):void {
debug = val;
if (val) {
log("debug enabled");
}
}
-
+
private function loadDefaultPolicyFile(wsUrl:String):void {
var policyUrl:String = "xmlsocket://" + URLUtil.getServerName(wsUrl) + ":843";
log("policy file: " + policyUrl);
Security.loadPolicyFile(policyUrl);
}
-
+
public function loadManualPolicyFile(policyUrl:String):void {
log("policy file: " + policyUrl);
Security.loadPolicyFile(policyUrl);
manualPolicyFileLoaded = true;
}
-
+
public function log(message:String):void {
if (debug) {
- ExternalInterface.call("WebSocket.__log", encodeURIComponent("[WebSocket] " + message));
+ ExternalInterface.call("FlashWebSocket.__log", encodeURIComponent("[WebSocket] " + message));
}
}
-
+
public function error(message:String):void {
- ExternalInterface.call("WebSocket.__error", encodeURIComponent("[WebSocket] " + message));
+ ExternalInterface.call("FlashWebSocket.__error", encodeURIComponent("[WebSocket] " + message));
}
-
+
private function parseEvent(event:WebSocketEvent):Object {
var webSocket:WebSocket = event.target as WebSocket;
var eventObj:Object = {};
@@ -86,7 +86,7 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
}
return eventObj;
}
-
+
public function create(
webSocketId:int,
url:String, protocols:Array,
@@ -101,28 +101,28 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
newSocket.addEventListener("message", onSocketEvent);
webSockets[webSocketId] = newSocket;
}
-
+
public function send(webSocketId:int, encData:String):int {
var webSocket:WebSocket = webSockets[webSocketId];
return webSocket.send(encData);
}
-
+
public function close(webSocketId:int):void {
var webSocket:WebSocket = webSockets[webSocketId];
webSocket.close();
}
-
+
public function receiveEvents():Object {
var result:Object = eventQueue;
eventQueue = [];
return result;
}
-
+
private function getOrigin():String {
return (URLUtil.getProtocol(this.callerUrl) + "://" +
URLUtil.getServerNameWithPort(this.callerUrl)).toLowerCase();
}
-
+
private function getCookie(url:String):String {
if (URLUtil.getServerName(url).toLowerCase() ==
URLUtil.getServerName(this.callerUrl).toLowerCase()) {
@@ -131,7 +131,7 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
return "";
}
}
-
+
/**
* Socket event handler.
*/
@@ -140,18 +140,18 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
eventQueue.push(eventObj);
processEvents();
}
-
+
/**
* Process our event queue. If javascript is unresponsive, set
* a timeout and try again.
*/
public function processEvents():void {
if (eventQueue.length == 0) return;
- if (!ExternalInterface.call("WebSocket.__onFlashEvent")) {
+ if (!ExternalInterface.call("FlashWebSocket.__onFlashEvent")) {
setTimeout(processEvents, 500);
}
}
-
+
}
}
View
2  flash-src/src/net/gimite/websocket/WebSocketMainInsecure.as
@@ -14,7 +14,7 @@ public class WebSocketMainInsecure extends WebSocketMain {
Security.allowInsecureDomain("*");
super();
}
-
+
}
}
View
163 web_socket.js
@@ -4,17 +4,6 @@
// Reference: http://tools.ietf.org/html/rfc6455
(function() {
-
- if (window.WEB_SOCKET_FORCE_FLASH) {
- // Keeps going.
- } else if (window.WebSocket) {
- return;
- } else if (window.MozWebSocket) {
- // Firefox.
- window.WebSocket = MozWebSocket;
- return;
- }
-
var logger;
if (window.WEB_SOCKET_LOGGER) {
logger = WEB_SOCKET_LOGGER;
@@ -24,7 +13,7 @@
} else {
logger = {log: function(){ }, error: function(){ }};
}
-
+
// swfobject.hasFlashPlayerVersion("10.0.0") doesn't work with Gnash.
if (swfobject.getFlashPlayerVersion().major < 10) {
return;
@@ -44,11 +33,11 @@
* @param {int} proxyPort
* @param {string} headers
*/
- window.WebSocket = function(url, protocols, proxyHost, proxyPort, headers) {
+ window.FlashWebSocket = function(url, protocols, proxyHost, proxyPort, headers) {
var self = this;
- self.__id = WebSocket.__nextId++;
- WebSocket.__instances[self.__id] = self;
- self.readyState = WebSocket.CONNECTING;
+ self.__id = FlashWebSocket.__nextId++;
+ FlashWebSocket.__instances[self.__id] = self;
+ self.readyState = FlashWebSocket.CONNECTING;
self.bufferedAmount = 0;
self.__events = {};
if (!protocols) {
@@ -59,9 +48,9 @@
// 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.
self.__createTask = setTimeout(function() {
- WebSocket.__addTask(function() {
+ FlashWebSocket.__addTask(function() {
self.__createTask = null;
- WebSocket.__flash.create(
+ FlashWebSocket.__flash.create(
self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null);
});
}, 0);
@@ -72,8 +61,8 @@
* @param {string} data The data to send to the socket.
* @return {boolean} True for success, false for failure.
*/
- WebSocket.prototype.send = function(data) {
- if (this.readyState == WebSocket.CONNECTING) {
+ FlashWebSocket.prototype.send = function(data) {
+ if (this.readyState == FlashWebSocket.CONNECTING) {
throw "INVALID_STATE_ERR: Web Socket connection has not been established";
}
// We use encodeURIComponent() here, because FABridge doesn't work if
@@ -84,7 +73,7 @@
// preserve all Unicode characters either e.g. "\uffff" in Firefox.
// Note by wtritch: Hopefully this will not be necessary using ExternalInterface. Will require
// additional testing.
- var result = WebSocket.__flash.send(this.__id, encodeURIComponent(data));
+ var result = FlashWebSocket.__flash.send(this.__id, encodeURIComponent(data));
if (result < 0) { // success
return true;
} else {
@@ -96,18 +85,18 @@
/**
* Close this web socket gracefully.
*/
- WebSocket.prototype.close = function() {
+ FlashWebSocket.prototype.close = function() {
if (this.__createTask) {
clearTimeout(this.__createTask);
this.__createTask = null;
- this.readyState = WebSocket.CLOSED;
+ this.readyState = FlashWebSocket.CLOSED;
return;
}
- if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) {
+ if (this.readyState == FlashWebSocket.CLOSED || this.readyState == FlashWebSocket.CLOSING) {
return;
}
- this.readyState = WebSocket.CLOSING;
- WebSocket.__flash.close(this.__id);
+ this.readyState = FlashWebSocket.CLOSING;
+ FlashWebSocket.__flash.close(this.__id);
};
/**
@@ -118,7 +107,7 @@
* @param {boolean} useCapture
* @return void
*/
- WebSocket.prototype.addEventListener = function(type, listener, useCapture) {
+ FlashWebSocket.prototype.addEventListener = function(type, listener, useCapture) {
if (!(type in this.__events)) {
this.__events[type] = [];
}
@@ -133,7 +122,7 @@
* @param {boolean} useCapture
* @return void
*/
- WebSocket.prototype.removeEventListener = function(type, listener, useCapture) {
+ FlashWebSocket.prototype.removeEventListener = function(type, listener, useCapture) {
if (!(type in this.__events)) return;
var events = this.__events[type];
for (var i = events.length - 1; i >= 0; --i) {
@@ -150,7 +139,7 @@
* @param {Event} event
* @return void
*/
- WebSocket.prototype.dispatchEvent = function(event) {
+ FlashWebSocket.prototype.dispatchEvent = function(event) {
var events = this.__events[event.type] || [];
for (var i = 0; i < events.length; ++i) {
events[i](event);
@@ -163,15 +152,15 @@
* Handles an event from Flash.
* @param {Object} flashEvent
*/
- WebSocket.prototype.__handleEvent = function(flashEvent) {
-
+ FlashWebSocket.prototype.__handleEvent = function(flashEvent) {
+
if ("readyState" in flashEvent) {
this.readyState = flashEvent.readyState;
}
if ("protocol" in flashEvent) {
this.protocol = flashEvent.protocol;
}
-
+
var jsEvent;
if (flashEvent.type == "open" || flashEvent.type == "error") {
jsEvent = this.__createSimpleEvent(flashEvent.type);
@@ -186,12 +175,12 @@
} else {
throw "unknown event type: " + flashEvent.type;
}
-
+
this.dispatchEvent(jsEvent);
-
+
};
-
- WebSocket.prototype.__createSimpleEvent = function(type) {
+
+ FlashWebSocket.prototype.__createSimpleEvent = function(type) {
if (document.createEvent && window.Event) {
var event = document.createEvent("Event");
event.initEvent(type, false, false);
@@ -200,8 +189,8 @@
return {type: type, bubbles: false, cancelable: false};
}
};
-
- WebSocket.prototype.__createMessageEvent = function(type, data) {
+
+ FlashWebSocket.prototype.__createMessageEvent = function(type, data) {
if (document.createEvent && window.MessageEvent && !window.opera) {
var event = document.createEvent("MessageEvent");
event.initMessageEvent("message", false, false, data, null, null, window, null);
@@ -211,44 +200,44 @@
return {type: type, data: data, bubbles: false, cancelable: false};
}
};
-
+
/**
* Define the WebSocket readyState enumeration.
*/
- WebSocket.CONNECTING = 0;
- WebSocket.OPEN = 1;
- WebSocket.CLOSING = 2;
- WebSocket.CLOSED = 3;
+ FlashWebSocket.CONNECTING = 0;
+ FlashWebSocket.OPEN = 1;
+ FlashWebSocket.CLOSING = 2;
+ FlashWebSocket.CLOSED = 3;
// Field to check implementation of WebSocket.
- WebSocket.__isFlashImplementation = true;
- WebSocket.__initialized = false;
- WebSocket.__flash = null;
- WebSocket.__instances = {};
- WebSocket.__tasks = [];
- WebSocket.__nextId = 0;
-
+ FlashWebSocket.__isFlashImplementation = true;
+ FlashWebSocket.__initialized = false;
+ FlashWebSocket.__flash = null;
+ FlashWebSocket.__instances = {};
+ FlashWebSocket.__tasks = [];
+ FlashWebSocket.__nextId = 0;
+
/**
* Load a new flash security policy file.
* @param {string} url
*/
- WebSocket.loadFlashPolicyFile = function(url){
- WebSocket.__addTask(function() {
- WebSocket.__flash.loadManualPolicyFile(url);
+ FlashWebSocket.loadFlashPolicyFile = function(url){
+ FlashWebSocket.__addTask(function() {
+ FlashWebSocket.__flash.loadManualPolicyFile(url);
});
};
/**
* Loads WebSocketMain.swf and creates WebSocketMain object in Flash.
*/
- WebSocket.__initialize = function() {
-
- if (WebSocket.__initialized) return;
- WebSocket.__initialized = true;
-
- if (WebSocket.__swfLocation) {
+ FlashWebSocket.__initialize = function() {
+
+ if (FlashWebSocket.__initialized) return;
+ FlashWebSocket.__initialized = true;
+
+ if (FlashWebSocket.__swfLocation) {
// For backword compatibility.
- window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
+ window.WEB_SOCKET_SWF_LOCATION = FlashWebSocket.__swfLocation;
}
if (!window.WEB_SOCKET_SWF_LOCATION) {
logger.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
@@ -275,7 +264,7 @@
// Lite, we put it at (0, 0). This shows 1x1 box visible at left-top corner but this is
// the best we can do as far as we know now.
container.style.position = "absolute";
- if (WebSocket.__isFlashLite()) {
+ if (FlashWebSocket.__isFlashLite()) {
container.style.left = "0px";
container.style.top = "0px";
} else {
@@ -304,39 +293,39 @@
}
}
);
-
+
};
-
+
/**
* Called by Flash to notify JS that it's fully loaded and ready
* for communication.
*/
- WebSocket.__onFlashInitialized = function() {
+ FlashWebSocket.__onFlashInitialized = function() {
// We need to set a timeout here to avoid round-trip calls
// to flash during the initialization process.
setTimeout(function() {
- WebSocket.__flash = document.getElementById("webSocketFlash");
- WebSocket.__flash.setCallerUrl(location.href);
- WebSocket.__flash.setDebug(!!window.WEB_SOCKET_DEBUG);
- for (var i = 0; i < WebSocket.__tasks.length; ++i) {
- WebSocket.__tasks[i]();
+ FlashWebSocket.__flash = document.getElementById("webSocketFlash");
+ FlashWebSocket.__flash.setCallerUrl(location.href);
+ FlashWebSocket.__flash.setDebug(!!window.WEB_SOCKET_DEBUG);
+ for (var i = 0; i < FlashWebSocket.__tasks.length; ++i) {
+ FlashWebSocket.__tasks[i]();
}
- WebSocket.__tasks = [];
+ FlashWebSocket.__tasks = [];
}, 0);
};
-
+
/**
* Called by Flash to notify WebSockets events are fired.
*/
- WebSocket.__onFlashEvent = function() {
+ FlashWebSocket.__onFlashEvent = function() {
setTimeout(function() {
try {
// Gets events using receiveEvents() instead of getting it from event object
// of Flash event. This is to make sure to keep message order.
// It seems sometimes Flash events don't arrive in the same order as they are sent.
- var events = WebSocket.__flash.receiveEvents();
+ var events = FlashWebSocket.__flash.receiveEvents();
for (var i = 0; i < events.length; ++i) {
- WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
+ FlashWebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
}
} catch (e) {
logger.error(e);
@@ -344,30 +333,30 @@
}, 0);
return true;
};
-
+
// Called by Flash.
- WebSocket.__log = function(message) {
+ FlashWebSocket.__log = function(message) {
logger.log(decodeURIComponent(message));
};
-
+
// Called by Flash.
- WebSocket.__error = function(message) {
+ FlashWebSocket.__error = function(message) {
logger.error(decodeURIComponent(message));
};
-
- WebSocket.__addTask = function(task) {
- if (WebSocket.__flash) {
+
+ FlashWebSocket.__addTask = function(task) {
+ if (FlashWebSocket.__flash) {
task();
} else {
- WebSocket.__tasks.push(task);
+ FlashWebSocket.__tasks.push(task);
}
};
-
+
/**
* Test if the browser is running flash lite.
* @return {boolean} True if flash lite is running, false otherwise.
*/
- WebSocket.__isFlashLite = function() {
+ FlashWebSocket.__isFlashLite = function() {
if (!window.navigator || !window.navigator.mimeTypes) {
return false;
}
@@ -377,14 +366,14 @@
}
return mimeType.enabledPlugin.filename.match(/flashlite/i) ? true : false;
};
-
+
if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) {
// NOTE:
// This fires immediately if web_socket.js is dynamically loaded after
// the document is loaded.
swfobject.addDomLoadEvent(function() {
- WebSocket.__initialize();
+ FlashWebSocket.__initialize();
});
}
-
+
})();
Please sign in to comment.
Something went wrong with that request. Please try again.