timum-viw / socket.io-client

A socket.io-client implementation for ESP8266 and Arduino
228 stars 90 forks source link

Namespace support #34

Closed jslay88 closed 3 years ago

jslay88 commented 6 years ago

Would be really nice if we could get some namespace support.

I have tried to supply the namespace in the url and it still connects, but to the root namespace, and does not emit or respond to specified namespace.

I will try and take a look at the Socket.IO spec and see if I can't manage something myself, but I am not a strong C/Arduino coder.

mawildoer commented 5 years ago

Hi @jslay88,

Have you made any progress on this one? Happy to help, but having trouble finding the relevant sections of the socket.io docs/code

"doConnect" in here (socket.io repo) seems to be promising, but doesn't really contain the information as to when it's called

jslay88 commented 5 years ago

it is called on connect.

Client.prototype.connect = function(name, query){
  if (this.server.nsps[name]) {
    debug('connecting to namespace %s', name);
    return this.doConnect(name, query);
  }

  this.server.checkNamespace(name, query, (dynamicNsp) => {
    if (dynamicNsp) {
      debug('dynamic namespace %s was created', dynamicNsp.name);
      this.doConnect(name, query);
    } else {
      debug('creation of namespace %s was denied', name);
      this.packet({ type: parser.ERROR, nsp: name, data: 'Invalid namespace' });
    }
  });
};

Which I have to assume is the function you are calling when are calling io.connect()

Problem is, I am not the strongest JavaScript programmer, and I am having trouble following the code (particularly, where is this.server.checkNamespace server class actually defined).

mawildoer commented 5 years ago

In some experimenting, I found the request made here: (where the namespace is 'youll notice this'

image

mawildoer commented 5 years ago

This contains a bit more information regarding the encoding scheme, but honestly raises more questions than is answers: https://github.com/socketio/engine.io-parser/blob/master/lib/index.js

mawildoer commented 5 years ago

Okay, so based on: https://github.com/socketio/engine.io-parser/blob/master/lib/index.js

Lines >15

/**
 * Packet types.
 */

var packets = exports.packets = {
    open:     0    // non-ws
  , close:    1    // non-ws
  , ping:     2
  , pong:     3
  , message:  4
  , upgrade:  5
  , noop:     6
};

var packetslist = keys(packets);

/**
 * Premade error packet.
 */

var err = { type: 'error', data: 'parser error' };

/**
 * Encodes a packet.
 *
 *     <packet type id> [ <data> ]
 *
 * Example:
 *
 *     5hello world
 *     3
 *     4
 *
 * Binary is encoded in an identical principle
 *
 * @api private
 */

196

* Encodes multiple messages (payload).
*
*     <length>:data
*
* Example:
*
*     11:hello world2:hi

I've worked out that the above message in the request payload (last post) is 21 characters long, which is the first specifier (lines 196 onward) and the 4 in 40 signifies a message.

image

Specfically, according to the above, a connection message (the zero is processed elsewhere)

nis267 commented 4 years ago

Hello, someone knows how to connect to a namespace with this library or knows another library with which it's working ?

Thank you

coma0815 commented 4 years ago

https://github.com/timum-viw/socket.io-client/pull/81 There is a pull request - try to merge it in your library!

nis267 commented 4 years ago

Thank you @coma0815 for the fast answer it's working !