p2p / NodeJS:未logging的代码不会告诉我们如何通过networking发送消息

我们处于停滞状态,希望能够恢复活力。 我们唯一的问题是,它缺less这么多的文档,几乎无法使用。

我们可以连接到networking,当人们连接,断开连接以及所有人都在线时显示。

这里是有问题的回购。 要留意的文件是/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"); 

用于向networking上的每个人发送“node :: test”事件。 然后收到:

 node.on("node::test", function (message) { console.log("New message:",message); } 

但是这不行…任何想法?

从代码中可以看出,你应该这样做:

node.send(“test”,“message”)

另外,这里没有太多的东西……你可能会更好地重写你所需要的东西,而不是试图理解一个无证的小型库。 只是我2美分。

有人帮我find解决办法,看来它发送消息时发出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!");