简体   繁体   English

Socket.IO在Express 4路由上发出

[英]Socket.IO emit on Express 4 route

I'm trying to send messages to my all connected clients with socket.io from express 4 routes. 我正在尝试通过express.4路由将消息发送给我的所有具有socket.io的已连接客户端。 All my routes on same file, it's cloned from official socket.io chat example . 我所有的路由都在同一个文件中,它是从官方的socket.io chat example中克隆的。 I want to emit a "announcement" message to my all recently connected clients. 我想向所有最近连接的客户端发出“公告”消息。

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
    io.sockets.emit('chat message', "ANNOUNCEMENT : ..."); // <-- HERE
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log(msg);
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

But it's not emitting message and I don't see anything on console. 但是它没有发出消息,控制台上也看不到任何东西。 I've moved my routes to another file and passing io object to route as parameter, but it's still not working. 我已经将路由移动到另一个文件,并将io对象传递给route作为参数,但是仍然无法正常工作。 It's working if I emit message from client side JavaScript. 如果我从客户端JavaScript发出消息,它就可以正常工作。

"dependencies": {
    "express": "4.10.2",
    "socket.io": "1.2.0"
  }

Steps to reproduce problem with chat example 重现聊天示例问题的步骤

  1. git clone https://github.com/rauchg/chat-example.git
  2. cd chat-example && npm install
  3. nano index.js
  4. Change content of index.js with my code above 用上面的代码更改index.js的内容
  5. node index.js
  6. Browse to localhost:3000 , you should see a announcement on chat panel because I'm emitting it on app.get() route. 浏览到localhost:3000 ,您应该会在聊天面板上看到一条公告,因为我正在app.get()路由上app.get()该公告。 But there is no announcement. 但是没有公告。

You are emitting to the new page too soon in this: 您通过以下方式过早地进入了新页面:

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    io.sockets.emit('chat message', "ANNOUNCEMENT : ..."); // <-- HERE
});

The new page has not yet finished making it's socket.io connection so it won't get the .emit() . 新页面尚未完成与socket.io的连接,因此不会获得.emit() In fact, because res.sendFile() is asynchronous, the new page may not have even started making its socket.io connection yet. 实际上,由于res.sendFile()是异步的,因此新页面甚至可能尚未开始建立其socket.io连接。

You're doing the .emit() as soon as the res.sendFile() is initiated, but if you put console.log() statements into your server code, you will see that that occurs BEFORE the connection arrives. 一旦启动res.sendFile() ,您就在执行.emit() ,但是如果将console.log()语句放入服务器代码中,您将看到在连接到达之前发生这种情况。 So, you're emitting before the client has actually finished making it's socket.io connection. 因此,您需要在客户端实际完成其socket.io连接之前发出通知。

If you intend to send something upon page initialization, then you should do it in the server-side io.on('connection', ...) handler because that's where you know the new page is now connected. 如果打算在页面初始化时发送某些内容,则应在服务器端io.on('connection', ...)处理程序中进行处理,因为这是您现在知道新页面已连接的地方。

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

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