如何将 socket.io 实例传递给 typegraphql 解析器?

How to pass socket.io instance to typegraphql resolver?

我需要从 typegraphql 解析器向房间客户端发送一个事件。我试图在index.js中modules.export = io,但它创建了一个新实例,不一样。结果,它不起作用

更有可能你需要分享更多的代码才能确定,但​​这是我经常做的事情,比如:

// io.index.js

// setup io first 

const io = require("socket.io")();
// or
const { Server } = require("socket.io");
const io = new Server();

// then export it

module.exports = io;

然后在另一个文件中使用它,例如

// someotherfile.js

const io = require('./io.index.js');
io.emit("an event sent to all connected clients");

// or
io.to("room").emit("an event sent to all connected clients in room");

这样它总是相同的 io 实例。