自定义 node-red-node socket.io 客户端建立连接但在收到数据时崩溃?
Posted
技术标签:
【中文标题】自定义 node-red-node socket.io 客户端建立连接但在收到数据时崩溃?【英文标题】:custom node-red-node socket.io client makes connection but crashed when data is received? 【发布时间】:2015-08-03 00:42:29 【问题描述】:我是 Node-Red 和 socket.io 的新手。我想构建一个自定义节点作为从服务器接收 socket.io 消息的客户端。我称它为 socketi,因为它只是输入。该节点开始运行,使连接正常,但是当我将数据从另一个客户端发送到服务器到部署的 Node-Red 流时崩溃。这是节点的js:
module.exports = function(RED)
"use strict";
var socket = require("socket.io-client");
var io = require("socket.io-client");
function socketiNode(config)
RED.nodes.createNode(this,config);
var node = this;
socket = io.connect('http://192.168.2.3:8080');
socket.on('mouse',
function(data)
// When we receive data
this.warn([ data.x , data.y ]);
this.send([ data.x , data.y ]);
);
RED.nodes.registerType("socketi",socketiNode);
经过反复试验,我发现当我需要“socket.io-clinet”而不是“socket.io”的变量时会建立连接。它仍然需要其他模块,所以我在 node_modules 文件夹中有以下内容:
/node_modules
/debug
/engine.io
/has-binary-data
/socket.io-adapter
/socket.io-client
/socket.io-parser
我有 socket.io 服务器和另一个客户端在 Mac 上运行,Node-Red 流部署在 Raspberry Pi 2 上,都是本地的。以下是我运行 Node-Red 时发生的情况:
pi@raspberrypi ~ $ node-red-pi --max-old-space-size=128 -v
Welcome to Node-RED
===================
2 Aug 19:58:22 - [info] Node-RED version: v0.11.1
2 Aug 19:58:22 - [info] Node.js version: v0.12.0
2 Aug 19:58:22 - [info] Loading palette nodes
2 Aug 19:58:32 - [info] Settings file : /usr/local/lib/node_modules/node-red/settings.js
2 Aug 19:58:32 - [info] User directory : /home/pi/.node-red
2 Aug 19:58:32 - [info] Flows file : /home/pi/.node-red/flows_raspberrypi.json
2 Aug 19:58:32 - [info] Server now running at http://127.0.0.1:1880/
2 Aug 19:58:32 - [info] Starting flows
2 Aug 19:58:32 - [info] Started flows
2 Aug 19:58:41 - [info] Stopping flows
2 Aug 19:58:41 - [info] Stopped flows
2 Aug 19:58:41 - [info] Starting flows
2 Aug 19:58:41 - [info] Started flows
2 Aug 19:59:36 - [red] Uncaught Exception:
2 Aug 19:59:36 - TypeError: undefined is not a function
at Socket.<anonymous> (/home/pi/.node-red/nodes/node-red-node-socketi/socketi.js:12:22)
at Socket.Emitter.emit (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/node_modules/component-emitter/index.js:134:20)
at Socket.onevent (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/lib/socket.js:254:10)
at Socket.onpacket (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/lib/socket.js:212:12)
at Manager.<anonymous> (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/node_modules/component-bind/index.js:21:15)
at Manager.Emitter.emit (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/node_modules/component-emitter/index.js:134:20)
at Manager.ondecoded (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/lib/manager.js:301:8)
at Decoder.<anonymous> (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-client/node_modules/component-bind/index.js:21:15)
at Decoder.Emitter.emit (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
at Decoder.add (/home/pi/.node-red/nodes/node-red-node-socketi/node_modules/socket.io-parser/index.js:247:12)
pi@raspberrypi ~ $
19:59:36 是我将数据从客户端发送到服务器到 Node-Red 的地方。似乎很清楚我需要更多模块,即使我认为我应该只需要客户端代码。也许我只是缺少一些简单的东西?
【问题讨论】:
你最好在邮件列表中提出这样的问题,因为它允许更多的来回。 groups.google.com/forum/#!forum/node-red 【参考方案1】:function(data)
中的this
不会指向节点对象而是socket回调对象。尝试进行此更改:
...
var node = this;
socket = io.connect('http://192.168.2.3:8080');
socket.on('mouse',
function(data)
// When we receive data
node.warn([ data.x , data.y ]);
node.send([ data.x , data.y ]);
);
...
【讨论】:
啊!那解决了它。非常感谢。以上是关于自定义 node-red-node socket.io 客户端建立连接但在收到数据时崩溃?的主要内容,如果未能解决你的问题,请参考以下文章
7.FLINK Source基于集合基于文件基于Socket自定义Source--随机订单数量自定义Source自定义Source-MySQL