2

据我了解,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()

4

1 回答 1

4

Socket.IO 不是 WebSocket 的实现,它是在 HTTP 和 WebSocket 之上实现的不同协议。Socket.IO 客户端只能连接到 Socket.IO 服务器,反之亦然。WebSocket 协议与 Socket.IO 不兼容。

于 2019-04-07T15:47:34.247 回答