Skip to content

Commit

Permalink
Rename JS WebSocket class to FlashWebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
pl committed Mar 14, 2013
1 parent 9f7db36 commit 2ea12f6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 110 deletions.
Binary file modified WebSocketMain.swf
Binary file not shown.
Binary file modified WebSocketMainInsecure.zip
Binary file not shown.
44 changes: 22 additions & 22 deletions flash-src/src/net/gimite/websocket/WebSocketMain.as
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {};
Expand All @@ -86,7 +86,7 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
}
return eventObj;
}

public function create(
webSocketId:int,
url:String, protocols:Array,
Expand All @@ -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()) {
Expand All @@ -131,7 +131,7 @@ public class WebSocketMain extends Sprite implements IWebSocketLogger{
return "";
}
}

/**
* Socket event handler.
*/
Expand All @@ -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);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class WebSocketMainInsecure extends WebSocketMain {
Security.allowInsecureDomain("*");
super();
}

}

}
Loading

0 comments on commit 2ea12f6

Please sign in to comment.