-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZmqSocket.js
144 lines (121 loc) · 3.4 KB
/
ZmqSocket.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
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
(function ()
{
// Check if module is already initialized.
if (window.ZmqSocket)
return;
// Constructor. You may pass socket identity here.
ZmqSocket = function (identity)
{
this.fd = ZmqSocket.__nextFd++;
ZmqSocket.__sockets[this.fd] = this;
this.identity = identity;
this.state = ZmqSocket.CONNECTING;
this.onopen = function () {};
this.onmessage = function () {};
this.onerror = function (msg) {};
this.onclose = function () {};
var athis = this;
ZmqSocket.__addTask (function ()
{
ZmqSocket.__flash.create (athis.fd, athis.identity);
});
};
ZmqSocket.__sockets = [];
ZmqSocket.__tasks = [];
ZmqSocket.__nextFd = 0;
ZmqSocket.__flash = null;
ZmqSocket.CONNECTING = 1;
ZmqSocket.OPEN = 2;
ZmqSocket.CLOSING = 3;
// Called by Flash when it's ready.
ZmqSocket.__onFlashReady = function ()
{
if (navigator.appName.indexOf("Microsoft") != -1)
ZmqSocket.__flash = window["ZmqSocketFlash"];
else
ZmqSocket.__flash = document["ZmqSocketFlash"];
// Make it later to avoid recursion.
ZmqSocket.__later (function ()
{
for (var i = 0; i < ZmqSocket.__tasks.length; i++)
ZmqSocket.__tasks[i]();
});
};
// Used to make calls in a separate event handler to avoid
// recursive calls to Flash - recursion is not supported by many browsers.
ZmqSocket.__later = function (task)
{
var to = setTimeout (function () { clearTimeout (to); task (); }, 0);
}
// Called by Flash to find out if JS is ready.
ZmqSocket.__isJSReady = function () { return true; };
// Called when we are not sure that flash is ready.
ZmqSocket.__addTask = function (task)
{
if (ZmqSocket.__flash)
task ();
else
ZmqSocket.__tasks.push (task);
}
ZmqSocket.prototype.connect = function (host, port)
{
var athis = this;
ZmqSocket.__addTask (function ()
{
ZmqSocket.__flash.connect (athis.fd, host, port);
});
}
ZmqSocket.prototype.close = function ()
{
this.state = ZmqSocket.CLOSING;
var athis = this;
ZmqSocket.__addTask (function ()
{
alert('before');
ZmqSocket.__flash.close (athis.fd);
alert('after');
});
}
ZmqSocket.prototype.available = function ()
{
if (this.state != ZmqSocket.OPEN)
throw "Invalid state, socket must be connected!";
return ZmqSocket.__flash.available (this.fd);
}
ZmqSocket.prototype.recv = function ()
{
if (this.state != ZmqSocket.OPEN)
throw "Invalid state, socket must be connected!";
return ZmqSocket.__flash.recv (this.fd);
}
ZmqSocket.prototype.send = function (msg)
{
if (this.state != ZmqSocket.OPEN)
throw "Invalid state, socket must be connected!";
return ZmqSocket.__flash.send (this.fd, msg);
}
ZmqSocket.__onopen = function (fd)
{
// Avoiding recursion
ZmqSocket.__later (function ()
{
ZmqSocket.__sockets[fd].state = ZmqSocket.OPEN;
ZmqSocket.__sockets[fd].onopen ();
});
}
ZmqSocket.__onmessage = function (fd)
{
// Avoiding recursion
ZmqSocket.__later (function () { ZmqSocket.__sockets[fd].onmessage (); });
}
ZmqSocket.__onerror = function (fd, msg)
{
// Avoiding recursion
ZmqSocket.__later (function () { ZmqSocket.__sockets[fd].onerror (msg); });
}
ZmqSocket.__onclose = function (fd)
{
// Avoiding recursion
ZmqSocket.__later (function () { ZmqSocket.__sockets[fd].onclose (); });
}
})();