javascript - 访问 Node-Red websocket 监听器

标签 javascript node.js websocket node-red

我的服务器嵌入了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:

enter image description here

问题涉及: 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/

相关文章:

javascript - $http.post 到 php 文件,500 内部服务器错误

javascript - Kinetic JS 火焰路径事件

javascript - 带有 readline 的函数在获得响应之前在 for 循环中运行 (Node.Js)

ssl - 如何在 HornetQ 中接受安全的 websocket 连接

javascript - 为什么我的 WebSocket 客户端总是在发送消息后重新加载?

meteor - Apollo 2.x 客户端和服务器设置 : Error during WebSocket handshake?

javascript - 在 ES6 类中通过 promise 设置属性

javascript - 在 ECMAScript 6 中创建可取消的 Promise

mysql - 无法从 Node Express 应用程序访问 mysql 数据库

javascript - Grunt 不执行 grunt-contrib-sass 任务