danni / uwebsockets

Micropython websockets implementation
MIT License
192 stars 44 forks source link

Connecting to socket.io: 400 Bad Request "unknown transport "undefined"" #19

Open alexwohlbruck opened 2 years ago

alexwohlbruck commented 2 years ago

I am trying to connect to my socket.io server, and Socket.io is returning a 400 error with the message unknown transport "undefined". It seems that the transport query param needs to be set. I tried setting it to websocket and polling but neither seem to get me anywhere. I don't know much about the connection protocol, but why might this be happening?

Server log:

listening on *:3000
  engine intercepting request for path "/socket.io/" +0ms
  engine handling "GET" http request "/socket.io/?EIO=3" +1ms
  engine unknown transport "undefined" +2ms

Micropython log:

b'GET %s HTTP/1.1' /socket.io/?EIO=3
b'Host: %s:%s' 192.168.86.31 3000
b''
Traceback (most recent call last):
  File "main.py", line 36, in <module>
  File "main.py", line 27, in main
  File "main.py", line 20, in hello
  File "usocketio/client.py", line 78, in connect
  File "usocketio/client.py", line 43, in _connect_http
AssertionError: b'HTTP/1.1 400 Bad Request'
MicroPython v1.18 on 2022-01-17; ESP32 module with ESP32
Type "help()" for more information.

My code, from example:

with usocketio.client.connect('http://192.168.86.31:3000') as socketio:
   @socketio.on('message')
   def on_message(self, message):
        print("message", message)

socketio.run_forever()
alexwohlbruck commented 2 years ago

When I change query param to websocket

b'GET %s HTTP/1.1' /socket.io/?EIO=3&transport=websocket
b'Host: %s:%s' 192.168.86.31 3000
b''
Traceback (most recent call last):
  File "main.py", line 36, in <module>
  File "main.py", line 27, in main
  File "main.py", line 20, in hello
  File "usocketio/client.py", line 78, in connect
  File "usocketio/client.py", line 43, in _connect_http
AssertionError: b'HTTP/1.1 400 Bad Request'
MicroPython v1.18 on 2022-01-17; ESP32 module with ESP32
Type "help()" for more information.
  engine intercepting request for path "/socket.io/" +6m
  engine handling "GET" http request "/socket.io/?EIO=3&transport=websocket" +2ms
  engine handshaking client "Od-b3kZOLtvyo5zcAAAA" +5ms
  engine error handshaking to transport "websocket" +1ms

And when I change it to polling

b'GET %s HTTP/1.1' /socket.io/?EIO=3&transport=polling
b'Host: %s:%s' 192.168.86.31 3000
b''
Traceback (most recent call last):
  File "main.py", line 36, in <module>
  File "main.py", line 27, in main
  File "main.py", line 20, in hello
  File "usocketio/client.py", line 78, in connect
  File "usocketio/client.py", line 55, in _connect_http
AssertionError:
MicroPython v1.18 on 2022-01-17; ESP32 module with ESP32
Type "help()" for more information.
engine intercepting request for path "/socket.io/" +57s
  engine handling "GET" http request "/socket.io/?EIO=3&transport=polling" +1ms
  engine handshaking client "8ackU3rdUi6lEBhqAAAB" +2ms
  engine:socket sending packet "open" ({"sid":"8ackU3rdUi6lEBhqAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}) +0ms
  engine:polling setting request +0ms
  engine:socket flushing buffer to transport +2ms
  engine:polling writing "96:0{"sid":"8ackU3rdUi6lEBhqAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}" +2ms
  engine:socket executing batch send callback +4ms
  socket.io:server incoming connection with id 8ackU3rdUi6lEBhqAAAB +7m
  socket.io:client connecting to namespace / +0ms
  socket.io:namespace adding socket to nsp / +0ms
  socket.io:socket socket connected - writing packet +0ms
  socket.io:socket join room 8ackU3rdUi6lEBhqAAAB +0ms
  socket.io:client writing packet {"type":0,"nsp":"/"} +5ms
  socket.io-parser encoding packet {"type":0,"nsp":"/"} +0ms
  socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +1ms
  engine:socket sending packet "message" (0) +9ms
A user connected