问题描述
我想知道是否可以从路由器控制器级别访问ws实例?
这是我的入口点:
// app.js
// .... app configs
const server = createServer(app);
const websocket = new WebSocketServer({ httpServer: server });
websocket.on("request",(request) => {
let connection = request.accept(null,request.origin);
connection.on("message",(message) => {
connection.send("message");
console.log(message);
});
connection.on("close",() => console.log("client closed"));
return connection
});
我在请求对象中做了一些修改,以访问每个请求对象中的ws实例:
app.use((req,res,next) => {
req.ws = websocket;
next();
});
所以我想知道如何使用:
发布消息req.ws.send({message: "hi"})
我用socket-io尝试了这个技巧。但是在这里我无法访问ws.send()函数。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)