我们处于停滞状态,并且希望将其恢复生机。我们唯一的问题是,它缺乏如此多的文档,几乎无法使用。
我们可以建立与网络的连接,显示人们何时连接,断开连接以及所有人都在线。
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);
}
但那不起作用......任何想法?
答案 0 :(得分:1)
从简短的代码看,您应该这样做:
node.send(" test"," message")
还有那么多......你可能最好只重写你需要的东西,而不是试图理解一个没有文档记录的小lib。只需2美分。
答案 1 :(得分:0)
有人帮我找到了解决方案,它似乎在发送消息时会发出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!");