1

I am making a game for TWO players with node.js, socket.io and express.

In order to avoid effect from the third person that come to play, i want to generate specific URL for the two who are ready to play.

So my question is, how to change the URL when two people have come?

I am now testing it in local. In the server side, i have server listen to a port.

 var express = require('express');
 var SocketIO = require('socket.io');
 var http = require('http');
 var path = require('path');

 var app = express({
   views: path.join(__dirname, 'views')
 });

 app.use('/assert', express.static(path.join(__dirname, 'assert')));
 app.engine('html', require('ejs').renderFile);

 app.get('/', function (req, res) {
   res.render('home.html', {layout: false});
 });

 var server = http.createServer(app);
 var sio = SocketIO.listen(server);
 server.listen(8080);  

 function(...){...}

In the client side, i have socket connected to somewhere.

 var socket = io.connect('http://' + location.host);
 var ....

i guess what i need to do is to provide a button when i detect that two clients come in. When they clicked it, it direct them to a generated new URL.

So there is an old URL and many new ones...

Then how can i set different URLs in both server and client??

Any help is appreciated^ ^

kikkpunk
  • 1,287
  • 5
  • 22
  • 34
  • So when two players have signalled that they want to play, you want to send them to a (unique?) URL for their game? – robertklep Jun 16 '13 at 15:02
  • @robertklep, just redirect them to the URL with generated parameter, in order to separate every two players. – kikkpunk Jun 17 '13 at 13:09

1 Answers1

1

There are many ways for generating a UNIQUE random url 1) Use Timestamp and append it to a predefined url of yours 2) Use random number generators. This can be done either by pure javascript function Math.Random() or even the one provided by node

Eg.

var url = "http://www.yourHostName.com/"+(Math.Random()*(UpperLimit-LowerLimit)+(LowerLimit));

Supply Large UpperLimit Like 100000 (According to your need and expected traffic) and LowerLimit = 0; or by using the time stamp like this:

var url = "http://www.yourHostName.com/"+ new Date.getTime();

Beside this as I said you can also use the node to do the same task. There is a module called node-uuid which can do the similar task Refer to this Question: generate URL on node.js server

Once you have the unique URL and the reference to the clients to whom you want to send to a same Generated URL. Then Just Redirect them using the following:

response.writeHead(301,
{Location: url};       //one you generated
);
response.end();
Community
  • 1
  • 1
sumitb.mdi
  • 1,010
  • 14
  • 17