【发布时间】:2021-08-30 19:22:41
【问题描述】:
我需要从 typegraphql 解析器向房间客户端发送一个事件。我尝试在 index.js 中modules.export = io,但它创建了一个新实例,不一样。结果,它不起作用
【问题讨论】:
-
您将不得不分享更多代码。
标签: javascript node.js socket.io typegraphql
我需要从 typegraphql 解析器向房间客户端发送一个事件。我尝试在 index.js 中modules.export = io,但它创建了一个新实例,不一样。结果,它不起作用
【问题讨论】:
标签: javascript node.js socket.io typegraphql
您可能需要分享更多代码才能确定,但这是我经常做的事情,例如:
// 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 实例。
【讨论】: