Answer the question
In order to leave comments, you need to log in
Why does a block of code fire multiple times?
app.get('/watch/:id',function(req,res){
roomid = req.params.id;
Rooms.find({"id":roomid},function(err,rooms){
if(err !== null)console.log(err);
//res.send(rooms);
res.render('index',{title:'Hello'});
console.log(rooms);
});
io.on('connection',function(socket){
console.log('socket '+socket.id.toString()+' was connected');
socket.on('join',function(r_id){
console.log('type of roomid '+typeof(roomid));
console.log('type of r_id '+typeof(r_id));
console.log('r_id='+r_id+'\n');
console.log('roomid='+roomid);
socket.join(r_id);
var clients_in_the_room = io.sockets.adapter.rooms[r_id];
for (var clientId in clients_in_the_room ) {
console.log('client: %s', clientId); //Seeing is believing
}
});
// socket.join(roomid);
socket.on('message',function(msg,room_id){
console.log('messg in '+room_id+' room\n'+'room_id type is ' + typeof(room_id));
var rid = room_id.toString();
io.to(room_id).emit('message_server',msg);
});
socket.on('message_server',function(msg){
console.log('message_server '+msg);
});
});
//res.sendfile('room.html')
});
io.on('connection',function(socket){
starts to be duplicated, and messages are displayed by 2, 3, etc. times. How can I make the message appear only once?
Answer the question
In order to leave comments, you need to log in
Because with each "app.get(...)" you hang an event on io.on('connection')...
On the first request, you hung this being 1 time, then 2nd, and so on.
Take out this code
io.on('connection',function(socket){
...
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question