Skip to content

Commit

Permalink
Merge pull request #48 from LearnBoost/improve/id-generation
Browse files Browse the repository at this point in the history
Improve/id generation
  • Loading branch information
rauchg committed Aug 6, 2012
2 parents b0a1c34 + fb522c4 commit 93d8c8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/server.js
Expand Up @@ -6,11 +6,12 @@
var qs = require('querystring')
, parse = require('url').parse
, readFileSync = require('fs').readFileSync
, crypto = require('crypto')
, transports = require('./transports')
, debug = require('debug')('engine')
, EventEmitter = require('events').EventEmitter
, Socket = require('./socket')
, WebSocketServer = require('ws').Server
, debug = require('debug')('engine')

/**
* Module exports.
Expand Down Expand Up @@ -118,7 +119,13 @@ Server.prototype.prepare = function (req) {
*/

Server.prototype.id = function () {
return String(Math.random() * Math.random()).substr(3);
var rand = new Buffer(15);
this.sequenceNumber = (this.sequenceNumber + 1) | 0;
rand.writeInt32BE(this.sequenceNumber, 11);
crypto.randomBytes(12).copy(rand);
var id = rand.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
if (this.clients[id]) return this.id();
return id;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions test/server.js
Expand Up @@ -55,7 +55,7 @@ describe('server', function () {
.send({ transport: 'polling' })
.end(function (res) {
// hack-obtain sid
var sid = res.text.match(/"sid":"([0-9]+)"/)[1];
var sid = res.text.match(/"sid":"([^"]+)"/)[1];
expect(res.headers['set-cookie'][0]).to.be('io=' + sid);
done();
});
Expand All @@ -67,7 +67,7 @@ describe('server', function () {
request.get('http://localhost:%d/engine.io/default/'.s(port))
.send({ transport: 'polling' })
.end(function (res) {
var sid = res.text.match(/"sid":"([0-9]+)"/)[1];
var sid = res.text.match(/"sid":"([^"]+)"/)[1];
expect(res.headers['set-cookie'][0]).to.be('woot=' + sid);
done();
});
Expand Down

0 comments on commit 93d8c8d

Please sign in to comment.