如何使用 python-socketio 从客户端访问我的套接字 ID?

How can I access my socket id from client with python-socketio?

我开始使用 python-socketio 连接 socketIO nodejs 服务器而不是 javascript,我遇到套接字 ID 问题:

class LoginSocket(socketio.ClientNamespace):
    def on_connect(self):
        print('connected to server')
        # I need get socket id here to emit to server


        # sio.disconnect()

    def on_python(self, *args, **kwargs):
        print(args, kwargs, 'xxxxxxxxxxxxxxxxxxxxxx')


    def on_disconnect(self):
        print('disconnected from server')


sio = socketio.Client()
sio.register_namespace(LoginSocket('/'))
if __name__ == '__main__':
    sio.connect('https:/sitename.com/')
    sio.wait()

..而且我无法从相同的 javascript 客户那里找到任何关于 socket.id 的文档。 在 javascript 连接成功后,我可以访问 socket.id,如下所示:

 const socket = io.connect('https://socketserver.com/', { 'forceNew': true });
      socket.on('connect', function () {
      alert(socket.id)
})

这似乎是我的遗漏。

您可以通过 sio.eio.sid 访问 sid,但我还会添加更多 high-level 选项。