尝试将我的 socketio 客户端从 js 移植到 python 但它不起作用

Trying to port my socketio client from js to python but it is not working

我有以下 JS 代码,非常适合通过 socketio 客户端库连接到套接字:

userApi = 'userapiaccesfskdglk';
userAccess = 'useaccesfjkasdfdlkf2';

var axios = require('axios');
var socket = require('socket.io-client')('wss://test-meownow12345.com:4566');

socket.emit('Authenticate', {api: userApi, access: userAccess});

socket.on('Party', function(party) {
    console.log('party', party)

})

这些不是实际使用的 userApi、userAccess 或 url,但使用它们仍然得到相同的意义。

这就是我在 python 中所拥有的,对我来说看起来像一个确切的端口,但没有工作:

from socketIO_client import SocketIO, LoggingNamespace
import logging
logging.basicConfig(level=logging.DEBUG)


def on_party():
    print('connect')

userApi = 'userapiaccesfskdglk'
userAccess = 'useaccesfjkasdfdlkf2'

with SocketIO('wss://test-meownow12345.com', 4566, LoggingNamespace) as socketIO:
    socketIO.emit('Authenticate', {'api': userApi, 'access': userAccess})
    socketIO.on('Party', on_party)

它似乎是等效的,但显然不是,因为代码无法通过以下打开 socket.io 连接的行:

with SocketIO('wss://test-meownow12345.com', 4566, LoggingNamespace) as socketIO:

在我的控制台中,它打印出以下重复的日志错误:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): wss
WARNING:socketIO-client:wss:4566//test-meownow12345.com/socket.io [engine.io waiting for connection] HTTPConnectionPool(host='wss', port=4566): Max retries exceeded with url: //test-meownow12345.com/socket.io/?EIO=3&transport=polling&t=1523668232386-0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x105000278>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))

我不太确定这是什么意思。

我尝试更改给出此消息的行并删除 'wss://' 部分以使其成为:

with SocketIO('test-meownow12345.com', 4566, LoggingNamespace) as socketIO:

但这仍然失败,尽管日志中有一条新消息重复:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): test-meownow12345.com
WARNING:socketIO-client:test-meownow12345.com:4566/socket.io [engine.io waiting for connection] ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): test-meownow12345.com
DEBUG:urllib3.connectionpool:Starting new HTTP connection (3): test-meownow12345.com

非常感谢任何帮助,这是一个非常令人沮丧的问题。

如果你看 SocketIO class

class SocketIO(EngineIO):

它继承自 EngineIO,其中 __init__

class EngineIO(LoggingMixin):

    def __init__(
            self, host, port=None, Namespace=EngineIONamespace,
            wait_for_connection=True, transports=TRANSPORTS,
            resource='engine.io', hurry_interval_in_seconds=1, **kw):
        self._is_secure, self._url = parse_host(host, port, resource)
        self._wait_for_connection = wait_for_connection

parse_host的定义是

def parse_host(host, port, resource):
    if not host.startswith('http'):
        host = 'http://' + host
    url_pack = parse_url(host)
    is_secure = url_pack.scheme == 'https'
    port = port or url_pack.port or (443 if is_secure else 80)
    url = '%s:%s%s/%s' % (url_pack.hostname, port, url_pack.path, resource)
    return is_secure, url

这表示用法应如下所示

with SocketIO('https://test-meownow12345.com', 4566, LoggingNamespace) as socketIO:
    socketIO.emit('Authenticate', {'api': userApi, 'access': userAccess})
    socketIO.on('Party', on_party)