我只需要socket.io
到emit
消息到clients
,如果一个新的对象被插入到数据库。因此,我的想法是直接从控制器的insert-method
发出消息。在我的server.js
文件中,我创建了socket.io object
,并尝试让其他模块可以访问它:
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
//make socket io accessible for other modules
module.exports.io = io;
在我的控制器中,我尝试以这种方式使用socket.io:
var io = require('../server').io;
...
io.sockets.on("connection", function(socket){
passportSocketIo.filterSocketsByUser(io, function (user) {
return user.workingAt === socket.handshake.user.workingAt;
}).forEach(function(s){
s.send("news", insertedObject);
});
});
现在我被卡住了。"connection“事件永远不会触发,因此不会发出消息。这是在separate
文件中使用socket.io
的正确方式吗?不幸的是,我找不到复杂的socket.io
示例。
https://stackoverflow.com/questions/19559135
复制相似问题