Answer the question
In order to leave comments, you need to log in
How to properly connect to socket.io?
I have a simple chat server that works fine
var express = require('express');
var socket = require('socket.io');
var app = express();
server = app.listen(5000,console.log('server is running on port 3000'));
io = socket(server);
io.on('connection', (socket) => {
let currentRoom = 'data'
socket.join(currentRoom )
// listen for the send message
event socket.on('MESSAGE', function(data){
//connect to a room (its id comes with the message)
socket.join(data.room)
//send the message
io.to(data.room).emit('RECEIVE_MESSAGE', data);
But instead of connecting and sending messages in one listener, I want to connect by separate listening
// in newRoom I pass the room id
socket.on('move to room', function (newRoom) {
socket.leave(currentRoom)
socket.join(newRoom)
currentRoom = newRoom
})
but instead of connecting to the id of the room passed to newRoom, this crap id connection takes from the id of its object (this.id)
How can I fix this or how can I pass the id of one listner function to another ????? ??????
Answer the question
In order to leave comments, you need to log in
Move all room variables to a higher level (to the parent scope).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question