简体   繁体   English

Node.js-Socket.io io.emit()什么都不做

[英]Node.js - Socket.io io.emit() doing nothing

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. 我的socket.io应用程序,io.emit()和io.sockets.emit()一直无法正常工作,它们什么也不做,也没有返回错误。

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. 如您所见,在客户端,test1,test2,test3和test4应该全部在客户端上打印,但是只有test3可以打印。

In addition, if you add the following to the on('connection') method 另外,如果将以下内容添加到on('connection')方法中

  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. IE,您不能使用这种方法使套接字加入一个房间。

Sockets belong to a certain namespace. 套接字属于某个命名空间。

By writing io.of('/projects/test_cases').on('connection', function(socket){}); 通过编写io.of('/projects/test_cases').on('connection', function(socket){}); you put all connected sockets to '/projects/test_cases' namespace. 您将所有已连接的套接字放入“ / projects / test_cases”命名空间。

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' 您发出与'/ 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. UPD:关于问题的第二部分,您可能想知道join是一个异步操作。 That's why room1 is not in socket.rooms right the next line of code. 这就是为什么room1不在下一行代码中的socket.rooms的原因。 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/ 另外,对我来说,文档的工作也还不错。.http: //socket.io/docs/和特定于名称空间的http://socket.io/docs/rooms-and-namespaces/

APP.JS 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 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. 要测试它在index.html文件的3个或4个选项卡中打开,然后在其中一个文件上按刷新5或6次,然后在控制台上检查其他选项卡,则应将其记录为“数据”两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM