Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I am using a msgpack mode, The python client can't connect to the Node.js local server. #861

Closed
Saifa-96 opened this issue Jan 29, 2022 · 2 comments

Comments

@Saifa-96
Copy link

Describe the bug
When I am using a msgpack mode, The python client can't connect to the Node.js local server.
The following is my server-side code:

// "socket.io": "^4.4.1",
// "socket.io-msgpack-parser": "^3.0.1"

import { Server } from 'socket.io';

const parser = require('socket.io-msgpack-parser');

class FrontSocket {
  readonly sio: Server;

  constructor() {
    this.sio = new Server({ parser });
  }

  start(port?: number) {
    this.sio.on('connection', (socket) => {
      console.log('create', socket.id);
    });

    this.sio.listen(port || 3000);
  }

  stop() {
    this.sio.disconnectSockets();
  }
}

const server = new FrontSocket();
server.start();

My client-side-python code:

# python = "^3.8"
# python-socketio = {extras = ["asyncio_client"], version = "^5.5.1"}
# msgpack = "^1.0.3"

import asyncio
import socketio

sio = socketio.AsyncClient(serializer='msgpack', logger=True, engineio_logger=True)

@sio.event
async def connect():
    print('connection established')

@sio.event
async def my_message(data):
    print('message received with ', data)
    await sio.emit('my response', {'response': 'my response'})

@sio.event
async def disconnect():
    print('disconnected from server')

async def main():
    await sio.connect('ws://localhost:3000')
    await sio.wait()

if __name__ == '__main__':
    asyncio.run(main())

Additional
I try to create a simple connection using Node.js, like this:

const socket = require('socket.io-client')('ws://localhost:3000', {
  parser: require('socket.io-msgpack-parser'),
});

The connection successfully established!
So, I Guess This problem is caused by the Python client.

Logs

Attempting polling connection to http://localhost:3000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': 'G94ku_bzdnzq1keOAAAA', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000}
Engine.IO connection established
Sending packet MESSAGE data <binary>
Attempting WebSocket upgrade to ws://localhost:3000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Unexpected error decoding packet: "'int' object is not subscriptable", aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Exiting read loop task
Connection failed, new attempt in 0.72 seconds
Attempting polling connection to http://localhost:3000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': '7ebRRtOdxMCdxHngAAAB', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000}
Engine.IO connection established
Sending packet MESSAGE data <binary>
Attempting WebSocket upgrade to ws://localhost:3000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Unexpected error decoding packet: "'int' object is not subscriptable", aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Exiting read loop task
Connection failed, new attempt in 0.74 seconds
Attempting polling connection to http://localhost:3000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': 'u8vBwPi-S91RDHawAAAC', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000}
Engine.IO connection established
Sending packet MESSAGE data <binary>
Attempting WebSocket upgrade to ws://localhost:3000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Unexpected error decoding packet: "'int' object is not subscriptable", aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Exiting read loop task
Connection failed, new attempt in 1.15 seconds
Connection failed, new attempt in 1.94 seconds
Attempting polling connection to http://localhost:3000/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': 'ex4z-tWqdlYpX35yAAAD', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 20000}
Engine.IO connection established
Sending packet MESSAGE data <binary>
Attempting WebSocket upgrade to ws://localhost:3000/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Unexpected error decoding packet: "'int' object is not subscriptable", aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Exiting read loop task
Connection failed, new attempt in 1.18 seconds
^CTraceback (most recent call last):
  File "/Users/saifa_96/opt/anaconda3/envs/study/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/saifa_96/opt/anaconda3/envs/study/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Users/saifa_96/Documents/python/poetry-study/backend_socket.py", line 20, in main
    await sio.connect('ws://localhost:3000')
  File "/Users/saifa_96/opt/anaconda3/envs/study/lib/python3.8/site-packages/socketio/asyncio_client.py", line 163, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
@miguelgrinberg
Copy link
Owner

@Saifa-96 can I ask you to retest using the main branch in this repo for your client?

The issue appears to be related to not passing any authentication data with your connection request. The Python server sends None when there is no auth, and the JS server does not seem to like that and closes the connection. Now I'm passing an empty object/dict like the Node client, and that seems to work.

@Saifa-96
Copy link
Author

@miguelgrinberg Yes! It works!
Thank you for your help! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants