forked from suxianbaozi/websocket4ie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
75 lines (61 loc) · 2.42 KB
/
socket.js
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
var handlers = {
'connects':[],
'onClose':function(index,flag) {
this.connects[index.replace("socket_","")].onclose();
},
'onConnect':function(index) {
this.connects[index.replace("socket_","")].onopen();
},
'onData':function(index,text) {
this.connects[index.replace("socket_","")].onmessage(text);
},
'debug':function(index,str) {
console.log(str);
}
};
function socket4ie() {
this.debug = 0;
this.init = function() {
this.index = handlers.connects.length;
handlers.connects.push(this);
}
this.connect = function(domain,port) {
this.createFlash(domain,port);
}
this.createFlash = function(domain,port) {
var html = '<object id="socket_'+this.index+'" type="application/x-shockwave-flash" data="websocket4ie.swf" width=0 height=0 class="swfupload">\
<param name="wmode" value="window">\
<param name="movie" value="websocket4ie.swf">\
<param name="quality" value="high">\
<param name="menu" value="false">\
<param name="allowScriptAccess" value="always">\
<param name="flashvars" value="movieName=socket_'+this.index+'&handlers=handlers&server='+domain+'&port='+port+'&debug='+this.debug+'"></object>';
var div = document.createElement('div');
div.id = "flash_"+this.index;
div.innerHTML = html;
document.body.appendChild(div);
}
this.onclose = function() {
}
this.onopen = function() {
}
this.onmessage = function(text) {
}
this.send = function(text) {
this.callFlash("sendData",[text]);
}
this.callFlash = function (functionName, argumentArray) {
argumentArray = argumentArray || [];
var movieElement = document.getElementById("socket_"+this.index);
var returnValue, returnString;
// Flash's method if calling ExternalInterface methods (code adapted from MooTools).
try {
returnString = movieElement.CallFunction('<invoke name="' + functionName + '" returntype="javascript">' + __flash__argumentsToXML(argumentArray, 0) + '</invoke>');
returnValue = eval(returnString);
} catch (ex) {
throw "Call to " + functionName + " failed";
}
return returnValue;
};
this.init();
}