我的服务器嵌入了node-red。我正在尝试在服务器中创建新的 websocket 监听器。但是,当执行此代码时,node-red 应用程序中的 websockets 停止工作。
const WebSocket = require('ws');
const wss = new WebSocket.Server({
server: server,
path: '/test'
});
wss.on('connection', function connection(ws, req) {
console.log('test');
});
node-red 管理面板中的 Websocket:
问题涉及: https://github.com/websockets/ws/issues/381
如何访问node-red websocket并处理自己路径的消息?
最佳答案
我知道这是一个旧线程,但我想我应该添加你可以在红色 Node 中使用操作代码,如下所示:
var WebSocket = global.get('ws');
const wss = new WebSocket.Server({
server: <your http(s) server>,
path: '/'
});
wss.on('connection', function connection(ws, req) {
node.warn('connection');
});
您只需要:
npm install ws
edit settings.js
在 functionGlobalContext 下:添加
ws:require('ws')
它确实有效,我这样使用它是因为我无法让 websocket Node 在我的配置中工作。
关于javascript - 访问 Node-Red websocket 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47096578/