-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsioclient.py
329 lines (278 loc) · 10.3 KB
/
sioclient.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import json
from threading import Thread, Event
from urllib import urlopen, urlencode
from websocket import create_connection
class Handler(object):
"""
SocketIO client handler.
Subclass this method with "on_name" for each event "name"
that you expect to receive from the server. Spaces in
event names will be converted to underscores.
The event handler is run in a listener thread.
"""
def on(self, message, callback):
setattr(self, "on_"+message.replace(" ","_"), callback)
# Standard messages
def on_connect(self, socket):
"""
Called when the server connects.
"""
def on_disconnect(self):
"""
Called when the server disconnects.
"""
def on_reconnect(self, *args):
print "reconnect with",args
def on_open(self,*args):
print "open with",args
def on_close(self,*args):
print "close with",args
def on_retry(self, *args):
print "retry with",args
def on_error(self, name, msg):
"""
Called when there is an error.
"""
print "error %s:"%name,msg
def on_message(self, msgid, msg):
"""
Called when a message is received. JSON messages will already
be decoded when this is called. Event signals will not signal
a recv message.
"""
print "unhandled message",msgid,msg
def unknown_event(self, name, *args):
"""
Called when there is no handler for a particular event.
The event name will already be converted to on_name, with spaces
in name converted to underscores.
"""
print "unknown event %s(%s)"%(name,", ".join("%r"%s for s in args))
def ack(self, msgid, event=None, args=[]):
"""
Server sent an acknowledgement to message. Ideally we should
never see this since the acknowledgement should trigger the
callback for a sent message, but that code hasn't been written yet.
"""
print "ack",msgid
DEFAULT_HANDLER = Handler()
PROTOCOL = 1
class SocketIO(object):
"""
SocketIO client connection.
"""
def __init__(self, host, port, handler=None):
self.host = host
self.port = int(port)
self.__do_handshake()
self._connect()
self.heartbeatThread = RhythmicThread(self.heartbeatTimeout - 2, self._send_heartbeat)
self.heartbeatThread.start()
self.handler = handler
self.special_handlers = {}
self.channels = {}
self.listenerThread = ListenerThread(self)
self.listenerThread.start()
def on(self, event, callback):
self.special_handlers[event] = callback
def get_handler(self, channel, event):
if channel:
source = self.channels[channel]
else:
source = self
if event in self.special_handlers:
return self.special_handlers[event]
if source.handler is None:
handler = DEFAULT_HANDLER
else:
handler = source.handler
name = 'on_'+event.replace(' ','_')
if hasattr(handler, name):
return getattr(handler, name)
else:
return lambda *args: handler.unknown_event(name, *args)
def __do_handshake(self):
try:
response = urlopen('http://%s:%d/socket.io/%s/' % (self.host, self.port,PROTOCOL))
except IOError:
raise SocketIOError('Could not start connection')
if 200 != response.getcode():
raise SocketIOError('Could not establish connection')
self.sessionID, heartbeatTimeout, connectionTimeout, supportedTransports = response.readline().split(':')
self.heartbeatTimeout = int(heartbeatTimeout)
self.connectionTimeout = int(connectionTimeout)
if 'websocket' not in supportedTransports.split(','):
raise SocketIOError('Could not parse handshake')
def _connect(self):
self.connection = create_connection('ws://%s:%d/socket.io/%s/websocket/%s' % (self.host, self.port, PROTOCOL, self.sessionID))
def __del__(self):
try:
self.heartbeatThread.cancel()
self.connection.close()
except AttributeError:
pass
def _send_heartbeat(self):
self.connection.send('2::')
def emit(self, eventName, *args, **kw):
"""
Signal an event on the server.
If channel keyword is specified, the event will be emitted on a
particular channel.
"""
channel = kw.pop("channel","")
msgid = kw.pop("msgid","")
if kw:
raise TypeError("Unknown keyword(s) "+", ".join(kw.keys()))
msg = json.dumps(dict(name=eventName, args=args))
#print "sending",msg
self.connection.send(':'.join(('5',str(msgid),channel,msg)))
def disconnect(self, channel=None):
"""
Close the socket, or close an individual channel to the socket if
channel is specified.
"""
self.connection.send('0::'+channel if channel else '0::')
if channel:
del self.channels[channel]
if not channel:
self.heartbeatThread.cancel()
self.listenerThread.cancel()
self.connection.close()
def connect(self, channel, handler=None, query=None):
"""
Connect to a channel in the socketIO server.
Returns a connection with emit/send methods for communicating with the
server.
"""
self.channels[channel] = Channel(self, channel, handler)
self.connection.send('1::'+channel+('?'+urlencode(query) if query else ""))
return self.channels[channel]
def send(self, msg, msgid="", channel=""):
"""
Send a messge to the socketIO server.
"""
if isinstance(msg, basestring):
code = '3'
data = msg
else:
code = '4'
data = json.dumps(msg)
self.connection.send(':'.join((code,msgid,channel,data)))
def wait(self):
"""
Wait for the event handler to terminate.
"""
self.listenerThread.join()
class Channel(object):
"""
Connection for sending messges to the socketIO server on a particular channel.
Note: does not yet support channel specific handlers.
"""
def __init__(self, socket, channel, handler):
self.socket = socket
self.channel = channel
self.handler = handler
self.special_handlers = {}
def disconnect(self):
self.socket.disconnect(channel=self.channel)
def emit(self, eventName, *args, **kw):
self.socket.emit(eventName, *args, channel=self.channel)
def send(self, msg):
self.socket.send(msg, channel=self.channel)
def on(self, event, callback):
self.special_handlers[event] = callback
class SocketIOError(Exception):
pass
class ListenerThread(Thread):
"""
Thread to process messages from the server.
"""
daemon = True
def __init__(self, socket):
super(ListenerThread,self).__init__()
self.done = Event()
self.socket = socket
def cancel(self):
"""Cancel the listener thread"""
self.done.set()
def run(self):
"""Run the listener thread"""
while not self.done.is_set():
msg = self.socket.connection.recv()
#print msg
if msg is None: break
split_data = msg.split(":",3)
if len(split_data) == 4:
code, msgid, channel, data = split_data
elif len(split_data) == 3:
code, msgid, channel = split_data
data = ''
elif len(split_data) == 1:
code = msg
msgid = channel = data = ''
else:
raise ValueError("message could not be parsed:\n "+msg)
if code == '5':
self.event(msgid, channel, data)
elif code == '0':
self.disconnect(channel)
elif code == '1':
self.connect(channel)
elif code == '3':
self.recv(msgid, channel, data)
elif code == '4':
self.recv(msgid, channel, json.loads(data))
elif code == '6':
self.ack(data)
elif code == '7':
self.error(channel, data)
def error(self, channel, message):
"""Notify handler that an error occurred"""
reason,advice = message.split('+',1)
handler = self.socket.get_handler(channel,'error')
handler(reason, advice)
def connect(self, channel):
"""Notify handler that the connection is available"""
handler = self.socket.get_handler(channel, 'connect')
handler(self.socket)
def disconnect(self, channel):
"""Notify hander that the connection has terminated"""
handler = self.socket.get_handler(channel, 'disconnect')
handler()
def event(self, msgid, channel, data):
"""Signal an event in the handler"""
event = json.loads(data)
handler = self.socket.get_handler(channel, event['name'])
handler(*event['args'])
def recv(self, msgid, channel, data):
"""Receive a message or a json message"""
handler = self.socket.get_handler(channel, 'message')
handler(msgid, data)
def ack(self, data):
"""Receive acknowledgement for an event"""
handler = self.socket.get_handler(channel, 'ack')
plus_idx = data.find('+')
if plus_idx > 0:
msgid, event = data[:plus_idx],json.loads(data[plus_idx+1:])
name = 'on_'+event['name'].replace(' ','_')
args = event['args']
handler(msgid, name, args)
else:
handler(msgid)
class RhythmicThread(Thread):
'Execute function every few seconds'
daemon = True
def __init__(self, intervalInSeconds, function, *args, **kw):
super(RhythmicThread, self).__init__()
self.intervalInSeconds = intervalInSeconds
self.function = function
self.args = args
self.kw = kw
self.done = Event()
def cancel(self):
self.done.set()
def run(self):
self.done.wait(self.intervalInSeconds)
while not self.done.is_set():
self.function(*self.args, **self.kw)
self.done.wait(self.intervalInSeconds)