admin管理员组文章数量:1278947
I'm having a problem with my socket.io applicaiton, the io.emit(), and io.sockets.emit() straight up do not work, they do nothing, and return no error.
I've provided here the most simplified version of the code I can make here.
Server:
var server = require('http').createServer();
var io = require('socket.io')(server);
var port = 8080;
io.use(function(socket, next){
next();
});
io.of('/projects/test_cases').on('connection', function(socket){
io.sockets.emit("test_case_changed", "test1"); // Doesn't do anything
io.emit("test_case_changed", "test2"); // Doesn't do anything
socket.emit("test_case_changed", "test3"); // Works
io.to(socket.id).emit("test_case_changed", "test4"); // doesn't work
});
io.on('connection', function(socket){
socket.on('disconnect', function(){
});
});
server.listen(port, function(){
console.log('listening on *:' + port);
setTimeout(function(){
io.sockets.emit("test_case_changed", "test"); // does nothing
io.emit("test_case_changed", "test"); // does nothing
}, 3000);
});
Client:
<script>
var socket = io('http://localhost:8080/projects/test_cases');
socket.on('error', function (reason){
console.error('Unable to connect Socket.IO', reason);
});
socket.on('connect', function (){
console.info('Connected');
});
socket.on("test_case_changed", function (data){
console.log(data);
});
</script>
As you can see, on the client side test1, test2, test3, and test4 should all print on the client, but only test3 does.
In addition, if you add the following to the on('connection') method
console.log("Pre join: ", socket.rooms);
socket.join("room1"); // Doesn't work
console.log("After join: ", socket.rooms);
That does not work either. IE you can't make the socket join a room in that method.
I'm having a problem with my socket.io applicaiton, the io.emit(), and io.sockets.emit() straight up do not work, they do nothing, and return no error.
I've provided here the most simplified version of the code I can make here.
Server:
var server = require('http').createServer();
var io = require('socket.io')(server);
var port = 8080;
io.use(function(socket, next){
next();
});
io.of('/projects/test_cases').on('connection', function(socket){
io.sockets.emit("test_case_changed", "test1"); // Doesn't do anything
io.emit("test_case_changed", "test2"); // Doesn't do anything
socket.emit("test_case_changed", "test3"); // Works
io.to(socket.id).emit("test_case_changed", "test4"); // doesn't work
});
io.on('connection', function(socket){
socket.on('disconnect', function(){
});
});
server.listen(port, function(){
console.log('listening on *:' + port);
setTimeout(function(){
io.sockets.emit("test_case_changed", "test"); // does nothing
io.emit("test_case_changed", "test"); // does nothing
}, 3000);
});
Client:
<script>
var socket = io('http://localhost:8080/projects/test_cases');
socket.on('error', function (reason){
console.error('Unable to connect Socket.IO', reason);
});
socket.on('connect', function (){
console.info('Connected');
});
socket.on("test_case_changed", function (data){
console.log(data);
});
</script>
As you can see, on the client side test1, test2, test3, and test4 should all print on the client, but only test3 does.
In addition, if you add the following to the on('connection') method
console.log("Pre join: ", socket.rooms);
socket.join("room1"); // Doesn't work
console.log("After join: ", socket.rooms);
That does not work either. IE you can't make the socket join a room in that method.
Share Improve this question edited Jun 8, 2015 at 21:36 user1032369 asked Jun 8, 2015 at 21:09 user1032369user1032369 5893 gold badges9 silver badges23 bronze badges 8-
isn't
io.sockets
an array of the connected sockets? – agconti Commented Jun 8, 2015 at 21:30 - @agconti - it's an object with some methods. A very confusing and mostly undocumented interface in socket.io. Great library, horrible documentation. – jfriend00 Commented Jun 8, 2015 at 21:31
- @agconti It is an object. That line does not return any undefined method error, or any error at all, and I have seen it in other people's implementations, so I believe it is a valid call. – user1032369 Commented Jun 8, 2015 at 21:35
- @jfriend00 ahh thanks for clearing that up. – agconti Commented Jun 8, 2015 at 21:37
- @jfriend00 its how you mount socket.io middleware. its only available in the source. – agconti Commented Jun 8, 2015 at 21:38
2 Answers
Reset to default 7Sockets belong to a certain namespace.
By writing io.of('/projects/test_cases').on('connection', function(socket){});
you put all connected sockets to '/projects/test_cases' namespace.
By writing
io.sockets.emit("test_case_changed", "test1");
io.emit("test_case_changed", "test2");
you emit to a default namespace '/' that is different from '/projects/test_cases'
You can easily see the namespace used:
console.log(io.sockets.name) // '/'
console.log(io.of('/projects/test_cases').name) // '/projects/test_cases'
You may want to explicitly specify namespace of sockets to emit messages to
io.of('/projects/test_cases').emit("test_case_changed", "test1");
UPD:
As for the second part of the question you may want to know that join
is an asynchronous operation. That's why room1
is not in socket.rooms
right the next line of code.
You may get notified when the socket is in the room using the following lines:
socket.join("room1", function(err){
console.log("After join: ", socket.rooms);
});
This worked for me. It is sending a message called 'Event' to all the connected sockets once another socket is connected.
I've just removed the unused code, so try doing that first and start from scratch.
Also, the documentation work for me is not that bad.. http://socket.io/docs/ and for namespaces specific http://socket.io/docs/rooms-and-namespaces/
APP.JS
var server = require('http').createServer();
var io = require('socket.io')(server);
var port = 8080;
var nsp = io.of('/projects/test_cases');
nsp.on('connection', function(socket) {
console.log('someone Connected!');
nsp.emit('event', 'data');
});
server.listen(port, function(){});
index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
var socket = io('http://localhost:8080/projects/test_cases');
socket.on('error', function (reason){
console.error('Unable to connect Socket.IO', reason);
});
socket.on('connect', function (){
console.info('Connected');
});
socket.on("event", function (data){
console.log(data);
});
</script>
</body>
</html>
To test it open in 3 or 4 tabs the index.html file and just press refresh 5 or 6 times on one of it, then check the console for the other tabs.. it should be logged 'data' a couple of times.
本文标签: javascriptNodejsSocketio ioemit() doing nothingStack Overflow
版权声明:本文标题:javascript - Node.js - Socket.io io.emit() doing nothing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235268a2362887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论