socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
60.93k stars 10.1k forks source link

Namespace mixed up with query parameters #3020

Closed xuyaoqiang closed 3 years ago

xuyaoqiang commented 7 years ago

I want to:

hi @darrachequesne : I'm having this issue link to #1003 (which is marked as unable to reporduce) when I'm using verison-2.0.3. after been tortured for all day and reviewed the code, I found the reason why the bug cannot be able to reproduce and confirm the bug existing.

Steps to reproduce

I'm using this to debug: https://github.com/darrachequesne/socket.io-fiddle/tree/issue/invalid-namespace and logging the server debug info:

$ npm run start

socket.io-fiddle@0.0.1 start /workspace/opensource/socket.io-fiddle DEBUG=socket.io* node server.js

socket.io:server initializing namespace / +0ms socket.io:server creating engine.io instance with opts {"path":"/socket.io"} +3ms socket.io:server attaching client serving req handler +18ms socket.io:server initializing namespace /device +8ms socket.io:server initializing namespace /portal +1ms socket.io-fiddle server listening at port 3000 +0ms socket.io:server incoming connection with id 6wicMdBitCbVpiAAAA +186ms socket.io:client connecting to namespace / +0ms socket.io:namespace adding socket to nsp / +1ms socket.io:socket socket connected - writing packet +2ms socket.io:socket joining room 6wicMdBitCbVpiAAAA +0ms socket.io:client writing packet {"type":0,"nsp":"/"} +1ms socket.io-parser encoding packet {"type":0,"nsp":"/"} +0ms socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +1ms socket.io:socket joined room 6wicMdBitCbVpiAAAA +1ms socket.io-parser decoded 0/device?deviceId=123456789abcdef as {"type":0,"nsp":"/device?deviceId=123456789abcdef"} +7ms socket.io:client no socket for namespace /device?deviceId=123456789abcdef, /device, [object Object] +7ms socket.io:client connecting to namespace /device +0ms socket.io:namespace adding socket to nsp /device +0ms socket.io:socket socket connected - writing packet +1ms socket.io:socket joining room /device#6wicMdBitCbVpiAAAA +0ms socket.io:client writing packet {"type":0,"nsp":"/device"} +0ms socket.io-parser encoding packet {"type":0,"nsp":"/device"} +1ms socket.io-parser encoded {"type":0,"nsp":"/device"} as 0/device +0ms

this log: socket.io-parser decoded 0/device?deviceId=123456789abcdef as {"type":0,"nsp":"/device?deviceId=123456789abcdef"} +7ms shows sockio server recieve frame: 0/device?deviceId=123456789abcdef and socket.io-parser decode the data to nsp as /device?deviceId=123456789abcdef

the reason connection still can be established is here :

Client.prototype.ondecoded = function(packet) {
  if (parser.CONNECT == packet.type) {
    this.connect(url.parse(packet.nsp).pathname, url.parse(packet.nsp, true).query);
  } else {
    var socket = this.nsps[packet.nsp];
    if (socket) {
      process.nextTick(function() {
        socket.onpacket(packet);
      });
    } else {
      debug('no socket for namespace %s', packet.nsp);
    }
  }
};

the connect packet was decoded with urlparser, but it is a hack fix.

So if you are testing with socket.io-server and socket.io-client together, it works fine. But it will broken if you are using other server framework that implemented socket.io-protocol like python-socketio like I was.

Setup

nicgirault commented 6 years ago

I think this is the issue I'm also facing with socket.io version: 2.0.4.

Here is my front code:

import io from 'socket.io-client'

const socket = io.connect(
  'api.example.com/reading',
  { query: 'language=en_US' }
)

The server listening on namespace reading does not receive any message. The suspect debug logs are:

socket.io-client:manager writing packet {"type":0,"query":"language=en_US","nsp":"/reading"}
socket.io-parser encoding packet {"type":0,"query":"language=en_US","nsp":"/reading?language=en_US"}
darrachequesne commented 3 years ago

For future readers: in Socket.IO v2, the query attribute of the Socket instance on the client-side was indeed appended to the namespace in the CONNECT packet.

This is not the case in Socket.IO v3 anymore. Documentation: