class mySockIo(object): def __init__(self, host, port, router): self.sock = SocketIO(host, port) self.sock.on(router, self.on_message) def on_message(self, *args): return args[0] if isinstance(args[0], dict) else None def send(self, data): self.sock.send(data) result = self.sock.wait_for_callbacks(2) return result def close(self): self.sock.disconnect()
"""http://websocke.server.com""" port = 3000 # 收到message消息处理过程 def on_message(*args): print("recv:", args) # print "geted:", type(args[0]) """ if type(args[0]) is types.DictType: rp = args[0] print("recv:", rp) """ """sk = SocketIO(hosts, port=port)""" sk = SocketIO(hosts, port=port, params={'token': 'ksdjfkjdf'}) # create connection with params # add lisenter for message response sk.on('message', on_message) data = {"sn": 0, "ver": 2} # send data to message sk.emit('message', data, on_message) sk.send(data, on_message) # default send data to message # send data to login sk.emit('login', data, on_message) sk.wait_for_callbacks(seconds=1)