我使用node.js进行游戏配对(例如,多个客户端连接服务器。如果有多个玩家,则它们彼此连接;否则将被踢30秒)。
目前,我正在使用socket进行连接(这可以检测意外丢失的连接)。但我想不出一种优雅的配对方式:
var net = require('net');
var _clients = new Array();
net.createServer(function(_socket) {
_socket.on('data', function(_data) {
//Parse client data
_clients.push(_socket);
setTimeout(function(){
_socket.write('kicked\n');
},30*1000);
_socket.on('close', function(data) {
//Delete _socket from _clients
}
}).listen(6969, '127.0.0.1');
setInterval(function(){
var pair = new Array();
while(_clients.length>2)
{
var _s1 = _clients.pop(), _s2 = _clients.pop();
// Pair _s1 & _s2
}
},2*1000);当前的代码可以工作,但它的设计确实很糟糕:
(1)使用SetInterval,而不是异步调用。(2)维护像_clients这样的数组是不方便的,因为我必须处理“踢开的”/lost连接/对或其他情况。
PS。目前我是按时间顺序配对客户,但可能需要随机配对或其他条件,以避免总是配对相同的人,而在线玩家不是那么多。
发布于 2012-07-04 18:14:48
为什么不使用下面这样的东西呢?
不带连接池的
var net = require('net')
, pendingPair = null;
net.createServer(function(_socket) {
_socket.on('data', function(_data) {
//Parse client data
if(!pendingPair) {
pendingPair = _socket;
} else {
// Now you have a pair!
var p1 = pendingPair
, p2 = _socket;
pendingPair = null;
}
}).listen(6969, '127.0.0.1');一旦建立了连接,您就会获得自动配对。您仍然需要在某个位置跟踪这些套接字,以便踢走客户端和断开连接,但您应该能够摆脱setIntervals。
具有连接池的
var net = require('net')
, _ = require('underscore')._
, clients = {}
, games = {};
function setKickTimer(socket) {
socket.kickTimer = setTimeout(function() {
socket.write('kicked\n');
}, 30 * 1000);
}
net.createServer(function(socket) {
socket.id = Math.floor(Math.random() * 1000);
setKickTimer(socket);
clients[socket.id] = socket;
socket.on('data', function(data) {
socket.data = parseData(data);
}
socket.on('close', function(data) {
var opponent = _.find(clients, function(client) { return client.opponentId === socket.id; });
// The opponent is no longer part of a pair.
if(opponent) {
delete opponent.opponentId;
setKickTimer(opponent);
}
delete clients[socket.id];
}
}).listen(6969, '127.0.0.1');
setInterval(function(){
// Get the client ids of clients who are not matched, and randomize the order
var unmatchedClientIds = _.shuffle(_.keys(_.filter(clients, function(client) { return !client.opponentId; })));
while(unmatchedClientIds > 2) {
var c1 = unmatchedClientIds.pop(),
, c2 = unmatchedClientIds.pop();
clearTimeout(clients[c1].kickTimer);
clearTimeout(clients[c2].kickTimer);
clients[c1].opponentId = c2;
clients[c2].opponentId = c1;
}
},2*1000);这不是一个完整的工作解决方案,但它应该让您了解如何管理丢弃的连接和将用户踢出队列。请注意,我使用了underscore.js来简化对集合的操作。
https://stackoverflow.com/questions/11281762
复制相似问题