-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZmqSocketMain.as
122 lines (104 loc) · 2.84 KB
/
ZmqSocketMain.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package
{
import flash.events.*;
import flash.display.Sprite;
import flash.external.*;
import flash.utils.*;
import ZmqSocket;
public class ZmqSocketMain extends Sprite
{
private var sockets: Array = new Array;
public function ZmqSocketMain ()
{
super ();
if (!ExternalInterface.available)
return;
ExternalInterface.addCallback ("create", create);
ExternalInterface.addCallback ("connect", connect);
ExternalInterface.addCallback ("close", close);
ExternalInterface.addCallback ("available", available);
ExternalInterface.addCallback ("recv", recv);
ExternalInterface.addCallback ("send", send);
if (!isJavaScriptReady ())
{
var timer: Timer = new Timer (100, 0);
timer.addEventListener (TimerEvent.TIMER, function (event: TimerEvent)
{
if (isJavaScriptReady ())
{
Timer (event.target).stop();
confirmFlashReady ();
}
});
timer.start ();
}
else
{
confirmFlashReady ();
}
}
private function isJavaScriptReady (): Boolean
{
try
{
return ExternalInterface.call ("ZmqSocket.__isJSReady");
}
catch (e: Error)
{
}
return false;
}
private function confirmFlashReady (): void
{
ExternalInterface.call ("ZmqSocket.__onFlashReady");
}
private function create (id: uint, identity: String): void
{
sockets[id] = new ZmqSocket (identity);
sockets[id].addEventListener (ZmqSocket.OPEN, function (e: Event) { onOpen (id); });
sockets[id].addEventListener (ZmqSocket.MESSAGE, function (e: Event) { onMessage (id); });
// These are propagated upwards.
sockets[id].addEventListener (Event.CLOSE, function (e: Event) { onClose (id); });
sockets[id].addEventListener (SecurityErrorEvent.SECURITY_ERROR,
function (e: SecurityErrorEvent) { onError (id, e); });
sockets[id].addEventListener (IOErrorEvent.IO_ERROR,
function (e: IOErrorEvent) { onError (id, e); });
}
private function connect (id: uint, host: String, port: uint): void
{
sockets[id].connect (host, port);
}
private function close (id: uint): void
{
sockets[id].close ();
}
private function available (id: uint): Number
{
return sockets[id].available ();
}
private function send (id: uint, msg: Array): void
{
sockets[id].send (msg);
}
private function recv (id: uint): Array
{
return sockets[id].recv ();
}
private function onOpen (id: uint): void
{
ExternalInterface.call ("ZmqSocket.__onopen", id);
}
private function onClose (id: uint): void
{
ExternalInterface.call ("ZmqSocket.__onclose", id);
}
private function onMessage (id: uint): void
{
ExternalInterface.call ("ZmqSocket.__onmessage", id);
}
private function onError (id: uint, e: Event): void
{
ExternalInterface.call ("ZmqSocket.__onerror", id, e.toString ());
}
}
}