admin管理员组文章数量:1023794
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
1 Answer
Reset to default 7socket.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
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
1 Answer
Reset to default 7socket.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
本文标签: javascriptSocketio broadcast to room not working in socketio v0913Stack Overflow
版权声明:本文标题:javascript - Socket.io broadcast to room not working in socket.io v.0.9.13 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745583573a2157451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论