Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:01:58+00:00 2026-06-07T05:01:58+00:00

I’m using node.js for gaming pairing(for example, several client connect the servers. They are

  • 0

I’m using node.js for gaming pairing(for example, several client connect the servers. They are connected to each other if there are more than one players; or they will be kicked for 30sec).

Currently, I’m using socket for connection (and this can detect lost connection unexpected). But I cannot figure out a elegant way for pairing:

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);

The current code works, but it is really bad designed 🙁

(1)SetInterval is used, rather than asynchronous call. (2)Maintaining an array like _clients is inconvenient, since I have to deal with “kicked”/lost connection/pair or other situations.

PS. Currently I’m pairing clients by time order, but maybe random pair or other condition is needed to avoid always pairing the same people when the online players are not so much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:02:00+00:00Added an answer on June 7, 2026 at 5:02 am

    Why not use something like the following?

    Without Connection Pool

    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');
    

    You get automatic pairs as soon as connections are made. You will still need to track those sockets somewhere for kicking clients, and dropped connections, but you should be able to get rid of the setIntervals.

    With Connection Pool

    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);
    

    This isn’t a complete working solution, but it should give you an idea of how to manage dropped connections and kicking users from the queue. Notice I’m using underscore.js to make the operations on the collections much easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.