我会在 python 中编写一个 websocket 客户端来连接到一个用 socket.io 编写的服务器。我当前的代码,取自1如下:
import websocket, httplib, sys, asyncore
def connect(server, port):
print("connecting to: %s:%d" %(server, port))
conn = httplib.HTTPConnection(server + ":" + str(port))
conn.request('POST','/socket.io/1/')
resp = conn.getresponse()
hskey = resp.read().split(':')[0]
ws = websocket.WebSocket(
'ws://'+server+':'+str(port)+'/socket.io/1/websocket/'+hskey,
onopen = _onopen,
onmessage = _onmessage,
onclose = _onclose)
return ws
def _onopen():
print("opened!")
def _onmessage(msg):
print("msg: " + str(msg))
def _onclose():
print("closed!")
if __name__ == '__main__':
server = 'localhost'
port = 8081
ws = connect(server, port)
try:
asyncore.loop()
except KeyboardInterrupt:
ws.close()
我的问题是如何连接到特定的命名空间?
谢谢