javascript - Socket.io broadcast to room not working in socket.io v.0.9.13 - Stack Overflow

socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' ha

socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');

is not hitting the client side. I can only see message from:

socket.emit('updateActivity', 'SERVER', 'you have CONNECTED to '+ socketEntity.roomName);

Did I do something wrong with socket.join()?

I also tried to give the broadcast event a different name instead of updateActivity, but it won't work either.

There is no mention in the log output about the broadcast emit at all.

Question Update:

I found a solution that if I replace broadcast.to() to the following snippet, it would work:

socket.get(socketEntity.roomId, function (error, room) {
   io.sockets.in(room).emit('updateActivity', 'SERVER', 'you -joined- this group '+ socketEntity.roomName);
});

But I don't know why that is the case at the moment...so somehow the room parameter for io.sockets.in() above isn't the same as the string socketEntity.roomId?

Original Code:

Server:

io.sockets.on ('connection', function (socket){
    socket.on('joinRoom', function(socketEntity){
        socket.join(socketEntity.roomId);
        socket.emit('updateActivity', 'SERVER', 'you have CONNECTED to '+ socketEntity.roomName);
        socket.broadcast.to(socketEntity.roomId).emit('updateActivity', 'SERVER', 'you -joined- this room'+ socketEntity.roomName);
    });
});

Client:

HTML:
<ul id="activityList" class="dropdown-menu"></ul>
JavaScript:
$(document).ready(function(){
    var socketEntity = {roomId:sampleRmId, roomName: "sample room"}
    socket.emit('joinRoom', socketEntity);


    socket.on('updateActivity', function (username, data){
        $('#activityList').prepend('<li><a href="#"><div>'+ data +'</div></a></li>');
    });
})
socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');

is not hitting the client side. I can only see message from:

socket.emit('updateActivity', 'SERVER', 'you have CONNECTED to '+ socketEntity.roomName);

Did I do something wrong with socket.join()?

I also tried to give the broadcast event a different name instead of updateActivity, but it won't work either.

There is no mention in the log output about the broadcast emit at all.

Question Update:

I found a solution that if I replace broadcast.to() to the following snippet, it would work:

socket.get(socketEntity.roomId, function (error, room) {
   io.sockets.in(room).emit('updateActivity', 'SERVER', 'you -joined- this group '+ socketEntity.roomName);
});

But I don't know why that is the case at the moment...so somehow the room parameter for io.sockets.in() above isn't the same as the string socketEntity.roomId?

Original Code:

Server:

io.sockets.on ('connection', function (socket){
    socket.on('joinRoom', function(socketEntity){
        socket.join(socketEntity.roomId);
        socket.emit('updateActivity', 'SERVER', 'you have CONNECTED to '+ socketEntity.roomName);
        socket.broadcast.to(socketEntity.roomId).emit('updateActivity', 'SERVER', 'you -joined- this room'+ socketEntity.roomName);
    });
});

Client:

HTML:
<ul id="activityList" class="dropdown-menu"></ul>
JavaScript:
$(document).ready(function(){
    var socketEntity = {roomId:sampleRmId, roomName: "sample room"}
    socket.emit('joinRoom', socketEntity);


    socket.on('updateActivity', function (username, data){
        $('#activityList').prepend('<li><a href="#"><div>'+ data +'</div></a></li>');
    });
})
Share Improve this question edited Mar 26, 2013 at 17:11 ttback asked Mar 26, 2013 at 16:24 ttbackttback 2,1115 gold badges29 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

socket.broadcast will send the message to all the other clients except the client it is being called on.

socket.emit sends to that particular client only

io.sockets.emit sends to all clients

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583576a4634396.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信