触发一个nodeJS socket.io服务器通过端口80广播

实时更新机制:用户写入 – > php保存在mysql数据库 – > php发送信息到nodeJS – > nodeJS发送更改所有订阅者 – >其他人可以实时注意到它。

Socket.io服务器运行良好,并运行在8080端口上。我有端口80上运行的节点http服务器。如何在http服务器上获取触发器消息并将消息发送到所有socket.io客户端?

var io = require('socket.io').listen(8080); var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Message Broadcast\n'); console.log("Message Sent"); //DOES NOT SEEM TO WORK, error io.broadcast("Messages"); //No error but no messages io.emit("Message"); }).listen(80, "0.0.0.0"); /** Other socket.io code which works well.. .... 

在Socket.IO 0.7中,您应该使用以下代替发送到所有连接的套接字,而不使用特定的套接字引用:

io.sockets.send("Messages")

请注意,这是不同的broadcast因为这将发送到所有套接字, 除了发起连接的套接字 – 当然你需要一个发送套接字引用,这就是为什么你当前的代码是不够的。

引自维基页面: Socket.IO/Wiki

如果你想发送消息给大家,你可以参考io.sockets

io.sockets.send('message');

io.sockets.emit('event');