据我了解,websocket
是协议,socketio
是实现该协议的库。
所以我决定从 pythonwebsocket-client
转移到python-socketio
使用 decorator 实现行为似乎更容易@sio.on('subject')
。
我正在使用该connect
方法的不同参数,但我总是遇到错误。
使用sio.connect('ws://echo.websocket.org')
orsio.connect('http://echo.websocket.org', transports=['websocket'])
错误为:
Attempting polling connection to http://echo.websocket.org/socket.io/?transport=polling&EIO=3
Traceback (most recent call last):
File "/home/lucas/projects/python/py-websockets/client/test.py", line 6, in <module>
sio.connect('ws://echo.websocket.org')
File "/home/lucas/.virtualenvs/py-websockets/lib/python3.6/site-packages/socketio/client.py", line 210, in connect
six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Unexpected status code 404 in server response
因此,查看我尝试过sio.connect('http://echo.websocket.org', transports=['websocket'], socketio_path='')
的日志,但只打印日志Attempting WebSocket connection to ws://echo.websocket.org//?transport=websocket&EIO=3
,然后它进入某种无限循环并且永远不会返回。
这是我正在尝试的代码:
import socketio
sio = socketio.Client(logger=True, engineio_logger=True)
@sio.on('connect')
def on_connect(*args, **kwargs):
print(args, kwargs)
if __name__ == '__main__':
sio.connect('http://echo.websocket.org', transports=['websocket'])
sio.wait()