简体   繁体   English

通过node.js的私人消息传递

[英]Private messaging through node.js

I'm making a multiplayer (2 player) browser game in JavaScript. 我正在用JavaScript制作多人(2人)浏览器游戏。 Every move a player makes will be sent to a server and validated before being transmitted to the opponent. 玩家所做的每一步动作都将发送到服务器并经过验证,然后再传送给对手。 Since WebSockets isn't ready for prime time yet, I'm looking at long polling as a method of transmitting the data and node.js looks quite interesting! 由于WebSockets尚未准备就绪,因此我正在考虑将长时间轮询作为一种传输数据的方法,node.js看起来很有趣! I've gone through some example code (chat examples, standard long polling examples and suchlike) but all the examples I've seen seem to broadcast everything to every client, something I'm hoping to avoid. 我已经看过一些示例代码(聊天示例,标准的长轮询示例等),但是我所看到的所有示例似乎都向每个客户端广播了所有内容,我希望避免这种情况。 For general server messages this is fine but I want two players to be able to square off in a lobby or so and go into "private messaging" mode. 对于一般的服务器消息,这很好,但是我希望两个玩家能够在大厅左右摆放并进入“私人消息传递”模式。

So I'm wondering if there's a way to implement private messaging between two clients using nodejs as a validating bridge? 所以我想知道是否有一种方法可以使用nodejs作为验证桥在两个客户端之间实现私有消息传递? Something like this: 像这样:

ClientA->nodejs: REQUEST
nodejs: VALIDATE REQUEST
nodejs->ClientA: VALID
nodejs->ClientB: VALID REQUEST FROM ClientA

You need some way to keep track of which clients are in a lobby together. 您需要某种方式来跟踪哪些客户在一起。 You can do this with a simple global array like so process.lobby[1] = Array(ClientASocket, ClientBSocket) or something similar (possibly with some additional data, like nicknames and such), where the ClientXSocket is the socket object of each client that connects. 您可以使用简单的全局数组(例如process.lobby[1] = Array(ClientASocket, ClientBSocket)或类似的方法(可能包含一些其他数据,例如昵称等)来完成此操作,其中ClientXSocket是每个客户端的套接字对象连接。

Now you can hook the lobby id (1 in this case) onto each client's socket object. 现在,您可以将大厅ID(在本例中为1)挂接到每个客户端的套接字对象上。 A sort of session variable (without the hassle of session ids) if you will. 如果可以的话,可以使用一种会话变量(没有会话ID的麻烦)。

// i just made a hashtable to put all the data in, 
// so that we don't clutter up the socket object too much.
socket.sessionData['lobby'] = 1;

What this allows you to do also, is add an event hook in the socket object, so that when the client disconnects, the socket can remove itself from the lobby array immediately, and message the remaining clients that this client has disconnected. 这还允许您做的是在套接字对象中添加一个事件挂钩,以便客户端断开连接时,套接字可以立即将其自身从游标阵列中删除 ,并向其余客户端发送此客户端已断开连接的消息。

// see link in paragraph above for removeByValue
socket.on('close', function(err) {
  process.lobby[socket.sessionData['lobby']].removeByValue(socket);
  // then notify lobby that this client has disconnected.
});

I've used socket in place of the net.Stream or request.connection or whatever the thing is. 我用socket代替net.Streamrequest.connection或其他东西。

Remember in HTTP if you don't have keep-alive connections, this will make the TCP connection close, and so of course make the client unable to remain within a lobby. 请记住,在HTTP中,如果没有保持活动的连接,则会使TCP连接关闭,因此当然会使客户端无法停留在大厅中。 If you're using a plain TCP connection without HTTP on top (say within a Flash application or WebSockets), then you should be able to keep it open without having to worry about keep-alive. 如果您使用的是普通的TCP连接,而顶部没有HTTP(例如在Flash应用程序或WebSockets中),那么您应该能够保持打开状态,而不必担心保持活动状态。 There are other ways to solve this problem than what I've shown here, but I hope I got you started at least. 除了我在这里展示的方法以外,还有其他方法可以解决此问题,但是我希望至少可以让您入门。 The key is keeping a persistent object for each client. 关键是为每个客户端保留一个持久对象。

Disclaimer: I'm not a Node.js expert (I haven't even gotten around to installing it yet) but I have been reading up on it and I'm very familiar with browser js, so I'm hoping this is helpful somehow. 免责声明:我不是Node.js专家(我什至还没有安装它)但是我一直在阅读它,并且我对浏览器js非常熟悉,所以我希望这会有所帮助不知何故。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM