forked from gimite/web-socket-js
/
WebSocketMain.as
98 lines (78 loc) · 2.87 KB
/
WebSocketMain.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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.display.*;
import flash.events.*;
import flash.external.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;
import mx.core.*;
import mx.controls.*;
import mx.events.*;
import mx.utils.*;
import bridge.FABridge;
public class WebSocketMain extends Sprite {
private var callerUrl:String;
private var debug:Boolean = false;
private var manualPolicyFileLoaded:Boolean = false;
public function WebSocketMain() {
// This is to avoid "You are trying to call recursively into the Flash Player ..."
// error which (I heard) happens when you pass bunch of messages.
// This workaround was written here:
// http://www.themorphicgroup.com/blog/2009/02/14/fabridge-error-you-are-trying-to-call-recursively-into-the-flash-player-which-is-not-allowed/
FABridge.EventsToCallLater["flash.events::Event"] = "true";
FABridge.EventsToCallLater["WebSocketMessageEvent"] = "true";
FABridge.EventsToCallLater["WebSocketStateEvent"] = "true";
var fab:FABridge = new FABridge();
fab.rootObject = this;
//log("Flash initialized");
}
public function setCallerUrl(url:String):void {
callerUrl = url;
}
public function setDebug(val:Boolean):void {
debug = val;
}
public function create(
url:String, protocol:String,
proxyHost:String = null, proxyPort:int = 0,
headers:String = null):WebSocket {
if (!manualPolicyFileLoaded) {
loadDefaultPolicyFile(url);
}
return new WebSocket(this, url, protocol, proxyHost, proxyPort, headers);
}
public function getOrigin():String {
return (URLUtil.getProtocol(this.callerUrl) + "://" +
URLUtil.getServerNameWithPort(this.callerUrl)).toLowerCase();
}
public function getCallerHost():String {
return URLUtil.getServerName(this.callerUrl);
}
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("webSocketLog", encodeURIComponent("[WebSocket] " + message));
}
}
public function error(message:String):void {
ExternalInterface.call("webSocketError", encodeURIComponent("[WebSocket] " + message));
}
public function fatal(message:String):void {
ExternalInterface.call("webSocketError", encodeURIComponent("[WebSocket] " + message));
throw message;
}
}
}