forked from kerryjiang/WebSocket4Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketEx.js
More file actions
148 lines (121 loc) · 4.36 KB
/
Copy pathWebSocketEx.js
File metadata and controls
148 lines (121 loc) · 4.36 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var app;
var silverlightHostID = 'silverlightControlHost';
var silverlightControlID = 'silverlightControl';
function WebSocketEx(uri, p1, p2, p3, p4, p5) {
this.uri = uri;
this.protocol = '';
if(typeof(p1) == 'function') {
this.onopen = p1;
this.onclose = p2;
this.onmessage = p3;
this.onerror = p4;
} else {
this.protocol = p1;
this.onopen = p2;
this.onclose = p3;
this.onmessage = p4;
this.onerror = p5;
}
this.websocket = null;
this.send = function (msg) {
websocket.send(msg);
}
function createBridgeApp() {
return Silverlight.createObject("ClientBin/WebSocket4Net.JsBridge.xap",
document.getElementById(silverlightHostID),
silverlightControlID,
{
width: "0",
height: "0",
background: "white",
version: "4.0.60310.0",
autoUpgrade: true
},
{
onError: onSilverlightError,
onLoad: onSilverlightLoaded
});
}
function createBirdgeWebSocket(host, proxy) {
var slPlugin = host;
if (slPlugin) {
websocket = slPlugin.content.services.createObject("WebSocket");
if (websocket) {
websocket.onopen = function (s, e) {
if (proxy.onopen != undefined)
proxy.onopen();
};
websocket.onclose = function (s, e) {
if (proxy.onclose != undefined) {
proxy.onclose();
}
};
websocket.onmessage = function (s, e) {
if (proxy.onmessage != undefined) {
proxy.onmessage(e);
}
};
return websocket;
}
}
}
var support = "MozWebSocket" in window ? 'MozWebSocket' : ("WebSocket" in window ? 'WebSocket' : null);
if (support != null) {
//using native websocket
var ws;
if (this.protocol && this.protocol.length > 0)
ws = new window[support](this.uri, this.protocol);
else
ws = new window[support](this.uri);
ws.onmessage = this.onmessage;
ws.onopen = this.onopen;
ws.onclose = this.onclose;
ws.onerror = this.onerror;
websocket = ws;
return;
}
if (Silverlight && Silverlight.isInstalled()) {
if (app == null || app == undefined) {
var proxy = this;
var protocol = proxy.protocol;
window.onSilverlightLoaded = function (sender, args) {
websocket = createBirdgeWebSocket(sender, proxy);
websocket.open(uri, protocol);
}
app = createBridgeApp();
return;
}
this.websocket = createBirdgeWebSocket();
this.websocket.open(this.uri, this.protocol);
return;
}
alert("Your browser cannot support WebSocket!");
}
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType == "ImageError" || errorType == "MediaError") {
return;
}
var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";
errMsg += "Code: " + iErrorCode + " \n";
errMsg += "Category: " + errorType + " \n";
errMsg += "Message: " + args.ErrorMessage + " \n";
if (errorType == "ParserError") {
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
throw new Error(errMsg);
}