我只需要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
示例。
发布于 2013-10-24 16:04:17
您正试图颠倒控制流程。方法是让你的控制器实现一个你的服务器可以用来传递控制的接口( API)。
一个简单的例子是:
在mycontroller.js
中
// no require needed here, at least, I don't think so
// Controller agrees to implement the function called "respond"
module.exports.respond = function(socket_io){
// this function expects a socket_io connection as argument
// now we can do whatever we want:
socket_io.on('news',function(newsreel){
// as is proper, protocol logic like
// this belongs in a controller:
socket.broadcast.emit(newsreel);
});
}
现在在server.js
中
var io = require('socket.io').listen(80);
var controller = require('./mycontroller');
io.sockets.on('connection', controller.respond );
这个例子很简单,因为控制器API看起来就像一个socket.io回调函数。但是,如果您想要将其他参数传递给控制器呢?例如io
对象本身或表示端点的变量?为此,您需要更多的工作,但这并不是很多。这基本上与我们经常用来打破或创建闭包的技巧相同:函数生成器:
在mycontroller.js
中
module.exports.respond = function(endpoint,socket){
// this function now expects an endpoint as argument
socket.on('news',function(newsreel){
// as is proper, protocol logic like
// this belongs in a controller:
endpoint.emit(newsreel); // broadcast news to everyone subscribing
// to our endpoint/namespace
});
}
现在在服务器上,我们需要做更多的工作才能通过端点:
var io = require('socket.io').listen(80);
var controller = require('./mycontroller');
var chat = io
.of('/chat')
.on('connection', function (socket) {
controller.respond(chat,socket);
});
注意,我们直接传递了socket
,但是我们通过闭包捕获了chat
。这样,您就可以拥有多个端点,每个端点都有自己的控制器:
var io = require('socket.io').listen(80);
var news_controller = require('./controllers/news');
var chat_controller = require('./controllers/chat');
var news = io
.of('/news')
.on('connection', function (socket) {
news_controller.respond(news,socket);
});
var chat = io
.of('/chat')
.on('connection', function (socket) {
chat_controller.respond(chat,socket);
});
实际上,您甚至可以为每个端点使用多个控制器。请记住,除了订阅事件之外,控制器不会做任何事情。监听的是服务器:
var io = require('socket.io').listen(80);
var news_controller = require('./controllers/news');
var chat_controller = require('./controllers/chat');
var chat = io
.of('/chat')
.on('connection', function (socket) {
news_controller.respond(chat,socket);
chat_controller.respond(chat,socket);
});
它甚至适用于普通的socket.io (没有端点/名称空间):
var io = require('socket.io').listen(80);
var news_controller = require('./controllers/news');
var chat_controller = require('./controllers/chat');
io.sockets.on('connection', function (socket) {
news_controller.respond(socket);
chat_controller.respond(socket);
});
发布于 2020-10-09 17:05:09
你可以很容易地做到这一点,你只需要在app.js中编写套接字连接,然后你就可以在你想要的任何地方使用套接字
在app.js
文件中放入如下代码
var http = require('http').createServer(app);
const io = require('socket.io')(http);
io.sockets.on("connection", function (socket) {
// Everytime a client logs in, display a connected message
console.log("Server-Client Connected!");
socket.join("_room" + socket.handshake.query.room_id);
socket.on('connected', function (data) {
});
});
const socketIoObject = io;
module.exports.ioObject = socketIoObject;
在任何文件或控制器中,您都可以像下面这样导入对象
const socket = require('../app'); //import socket from app.js
//you can emit or on the events as shown
socket.ioObject.sockets.in("_room" + req.body.id).emit("msg", "How are You ?");
发布于 2020-11-14 12:13:50
You can solve this problem declaring io instance as global variable.
at the last line of my app.js: IoInstance = require("./socket.io")(server);
at the './socket.io' :
const chatNamespace = require("./namespaces/chat");
const notificationsNamespace = require("./namespaces/notifications");
const auth = require("./middlewares/auth");
module.exports = (server) => {
const io = require("socket.io").listen(server);
io.of("/chats")
.use(auth)
.on("connect", (socket) => chatNamespace({ io, socket }));
io.of("/notifications")
.use(auth)
.on("connect", (socket) => notificationsNamespace({ io, socket }));
return io;
};
then, you can use the IoInstance wherever you want, event into your controller. Btw, I could have use it into my namespaces as well, but I've just realized it right now.
example of implementation in the testController.js:
module.exports = async (req, res) => {
IoInstance.of("/notifications")
.to("myUserId")
.emit("sendNotification", ["test", "test1"]);
return res.send("oioi");
};
https://stackoverflow.com/questions/19559135
复制相似问题