Lesterpig / sockpress

A simple express.js and socket.io wrapper for nodejs
MIT License
28 stars 9 forks source link

Room doesn't work #18

Closed anemol closed 6 years ago

anemol commented 6 years ago

I have 2 servers who use sockpress

I use Nginx as front proxy.

For the main website, i can use room without problem (join, broadcast to room)

For the game server, rooms don't work.

The main website provides the HTML/javacript and then, the javascript connects to the game server (socketio).

It works well since a while. But, recently, i wanted to use the room feature and i realized that it didn't work. There is no error.

app.io.route("/","ready", function( socket,version)
{
 ...
   socket.emit("preparing",data);
   socket.join("gaming");
});

setInterval(function()
{
    if (app && app.io)
    {
          app.io.in('gaming').emit("debugmessage","Hello gaming room!!!");
        }
}
},1000);

The debug message is never received on the client side.

Any idea?

Lesterpig commented 6 years ago

There is a test for the room feature (https://github.com/Lesterpig/sockpress/blob/master/test/scripts/http.js#L89). However, there may be a bug with the namespace system.

Can you try replacing app.io.route("/","ready", function( socket,version) with app.io.route("ready", function( socket,version)?

Alternatively, it may be a misuse of the ready event. You can also try to change the event name.

If nothing work, I'll investigate.

anemol commented 6 years ago

I applied the modifications but no change.

Finally, i found the source of my problem. To authenticate users, i check the validity of a simple token (given by the authoritative webserver) and i initialize a variable socket.id=token I renamed to "myId" instead "id" and now, it works

Anyway, thanks for your answer. :)

andreddosantos commented 6 years ago

I used this code to test:

function joinRoom(socket, room) { socket.join(room); socket.broadcast.to(room).emit('room joined', 1); //should not be sent socket.emit('room joined', 2); //should be sent app.io.to(room).emit('room joined', 3); //should be sent }

I realised that if had a route in a namespace context the rooms wouldn't work. app.io.route('/product', ioRoute); only receives 2 from joinRoom test function.

When using: app.io.route('/', ioRoute); i receive 2 and 3 from joinRoom test function.

So my question is, are the rooms segmented by namespace?

Can someone help please thanks

stevenkissack commented 6 years ago

Can you share the contents of ioRoute & where you are calling joinRoom from and what values are being passed for room (is it '/' & '/product')?

stevenkissack commented 6 years ago

I'll look into the code now as it has been a few years.. I'd suggest quickly logging the room and socket values in the joinRoom function to make sure it is passing the same values when it's mounted on the two different routes. From a first glance I can't see why the route namespaces would affect your custom room joining.

andreddosantos commented 6 years ago

Yup it seems so :/

"Rooms Within each namespace, you can also define arbitrary channels that sockets can join and leave."

https://socket.io/docs/rooms-and-namespaces/

my bad ;)

stevenkissack commented 6 years ago

Have you resolved your issue? One thought is to check your client side connection, and that you're setting the correct namespace to connect to on the server.

andreddosantos commented 6 years ago

I was able to get it to work by providing the namespace where there room is located.

app.io.of('/namespace').to('room').emit(....

But according to the socketio documentation (rooms are inside namespaces), so this is not a bug, i believe