-
Notifications
You must be signed in to change notification settings - Fork 1
/
sioclient.py
64 lines (61 loc) · 1.66 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
#-*-coding:utf-8-*-
import sioclientCpp as sio
class SioClient:
def __init__(self):
self.client=sio.Client()
self.queue={}
def start(self,uri,waitUntilConnected=False):
self.client.set_close_listener(self.onOpen)
self.client.set_socket_open_listener(self.onSocketOpen)
self.client.set_socket_close_listener(self.onSocketClose)
self.client.set_close_listener(self.onClose)
self.client.connect(uri)
if(waitUntilConnected):
while(not self.isConnected()):
import time
time.sleep(0.03)
def isConnected(self):
return self.client.opened()
def onOpen(self):
pass
def onFail(self):
pass
def onClose(self,reason):
pass
def onSocketOpen(self,nsp):
for e in self.eventList:
self.client.socket(nsp).on(e,self.onEvent)
self.client.socket(nsp).emit('enter_room',{'room':self.myRoom})
def onSocketClose(self,nsp):
pass
def onEvent(self,event):
name=event.get_name()
if(name in self.queue):
for q in self.queue[name]:
q.append([name,event.get_message()])
else:
self.defaultQueue.append([name,event.get_message()])
def setEventList(self,list):
self.eventList=list[:]
def setMyRoom(self,name):
self.myRoom=name
def setDataQueue(self,_queue,_eventList=None):
if(_eventList is None):
self.defaultQueue=_queue
else:
for e in _eventList:
if(e in self.queue):
self.queue[e].append(_queue)
else:
self.queue[e]=[_queue]
def emit(self,event,data,nsp=None):
if(nsp is None):
self.client.socket().emit(event,data)
else:
self.client.socket(nsp).emit(event,data)
def sendData(self,eventName,dstRooms,data):
self.client.socket().emit('transfer',{
'event':eventName,
'room':dstRooms,
'data':data
})