Uncaught TypeError: Cannot read property 'sid' of undefined

Uncaught TypeError: Cannot read property 'sid' of undefined

我在使用 python-socketiosocket.io-client 的 python-react 应用程序中使用 socket.io。客户端在连接到服务器时和触发 connect 事件之前显示错误。

服务器:

import asyncio
from aiohttp import web
import socketio
from aiohttp_middlewares import cors_middleware
from aiohttp_middlewares.cors import DEFAULT_ALLOW_HEADERS
sio = socketio.AsyncServer(async_mode='aiohttp',cors_allowed_origins='*')
app=web.Application()
sio.attach(app)

async def index(request):
    return web.Response(text="hello", content_type='text/html')

@sio.event
async def connect(sid, environ):
    print('Client connected',sid)
    await sio.emit('message', "good")

@sio.event
def disconnect(sid):
    print('Client disconnected',sid)

@sio.on('message')
async def print_message(sid, message):
    print("Socket ID: " , sid)
    print(message)

app.router.add_get('/', index)

if __name__ == '__main__':
    web.run_app(app)

客户端(反应):

import io from "socket.io-client"
let socket;    
.
.
.
componentDidMount(){
    socket = io('http://localhost:8080/', {transports:['websocket'], upgrade:false});
    socket.on('connect', ()=> {
        socket.on('message',(data)=>{console.log(data)})
    });
    socket.on('disconnect', () => {
        console.log(socket.connected);
    });
}

控制台打印错误:

这些文件与 socket.io-client

相关
Uncaught TypeError: Cannot read property 'sid' of undefined
    at Socket.onpacket (socket.js:189)
    at Manager.<anonymous> (index.js:21)
    at Manager.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:145)
    at Manager.ondecoded (manager.js:209)
    at Decoder.<anonymous> (index.js:21)
    at Decoder.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:145)
    at Decoder.add (index.js:117)
    at Manager.ondata (manager.js:201)
    at Socket.<anonymous> (index.js:21)
    at Socket.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:145)
    at Socket.onPacket (socket.js:387)
    at WS.<anonymous> (socket.js:196)
    at WS.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:145)
    at WS.onPacket (transport.js:103)
    at WS.onData (transport.js:96)
    at WebSocket.ws.onmessage (websocket.js:115)

我在使用最新版本的套接字时遇到了同样的错误。io-client Node.js。如果我使用像 v2.3.0 这样的旧版本,我不会收到任何错误。

我知道这个降级解决了一些人的问题,但如果你想使用最新版本(我不是 socket.io 方面的专家)请查看他们谈论的 post关于 API 中的一些更改并解决一些不一致的问题:

https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html