p2p/NodeJS: 未记录的代码没有告诉我们如何通过网络发送消息
p2p/NodeJS: Undocumented code does not tell us how to send messages through the network
我们正处于一个停滞不前的存储库中,希望将其恢复生机。我们唯一的问题是,它缺少太多文档,几乎无法使用。
我们能够连接到网络,显示人们何时连接、断开以及谁在线。
Here is the repo in question. 要关注的文件是 /lib/node.js(不要与 NodeJS 本身混淆)。
这是我们要展示的内容:
var Node = require('n2n').Node;
var node = new Node(5146);
console.log("Connecting to the network...\n\n\n");
node.connect([{ host: 'bradleypl.us', port: 5146 }]);
node.on('online', function () {
console.log('Your ID:', this.id);
console.log('Online ids:', node.sortedIds);
});
//just for testing, this will spam the terminal if repeated every time.
node.on('node::online', function (newNode) {
console.log('Someone is online:', newNode.id);
});
//just for testing, this will spam the terminal if repeated every time.
node.on('node::offline', function () {
console.log('Someone just left!');
});
这是我们不知道该怎么做的地方。现在如何发送消息?我们可以看到类似的东西:
node.broadcast("node::test","message");
用于向网络上的每个人发送 "node::test" 事件。然后收到:
node.on("node::test", function (message) {
console.log("New message:",message);
}
但这行不通...有什么想法吗?
简单看一下代码,您应该这样做:
node.send("test", "message")
那里也没有太多东西……你最好只重写你需要的东西,而不是试图理解一个未记录的小库。只是我的 2 美分。
有人帮我找到了解决办法,好像发消息的时候发出了2个参数。
//node::eventName, function handles senderID and data
node.on("node::test", function (sentFrom, message) {
console.log("New message:",message);
}
此外,要发送消息,您必须使用:
// userID, eventName, data (in this case, a string)
node.send("userid-342trw-tq34t3w-44q3t","test","Hello, World!");
我们正处于一个停滞不前的存储库中,希望将其恢复生机。我们唯一的问题是,它缺少太多文档,几乎无法使用。
我们能够连接到网络,显示人们何时连接、断开以及谁在线。
Here is the repo in question. 要关注的文件是 /lib/node.js(不要与 NodeJS 本身混淆)。
这是我们要展示的内容:
var Node = require('n2n').Node;
var node = new Node(5146);
console.log("Connecting to the network...\n\n\n");
node.connect([{ host: 'bradleypl.us', port: 5146 }]);
node.on('online', function () {
console.log('Your ID:', this.id);
console.log('Online ids:', node.sortedIds);
});
//just for testing, this will spam the terminal if repeated every time.
node.on('node::online', function (newNode) {
console.log('Someone is online:', newNode.id);
});
//just for testing, this will spam the terminal if repeated every time.
node.on('node::offline', function () {
console.log('Someone just left!');
});
这是我们不知道该怎么做的地方。现在如何发送消息?我们可以看到类似的东西:
node.broadcast("node::test","message");
用于向网络上的每个人发送 "node::test" 事件。然后收到:
node.on("node::test", function (message) {
console.log("New message:",message);
}
但这行不通...有什么想法吗?
简单看一下代码,您应该这样做:
node.send("test", "message")
那里也没有太多东西……你最好只重写你需要的东西,而不是试图理解一个未记录的小库。只是我的 2 美分。
有人帮我找到了解决办法,好像发消息的时候发出了2个参数。
//node::eventName, function handles senderID and data
node.on("node::test", function (sentFrom, message) {
console.log("New message:",message);
}
此外,要发送消息,您必须使用:
// userID, eventName, data (in this case, a string)
node.send("userid-342trw-tq34t3w-44q3t","test","Hello, World!");