Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ function sendErrorMessage (req, res, code) {
* @api public
*/

Server.prototype.generateId = function (req) {
return base64id.generateId();
Server.prototype.generateId = function (req, done) {
done(base64id.generateId());
};

/**
Expand All @@ -291,52 +291,53 @@ Server.prototype.generateId = function (req) {
*/

Server.prototype.handshake = function (transportName, req) {
var id = this.generateId(req);

debug('handshaking client "%s"', id);
var self = this;
var this_ = this;
this.generateId(req, function (id) {
debug('handshaking client "%s"', id);

try {
var transport = new transports[transportName](req);
if ('polling' === transportName) {
transport.maxHttpBufferSize = this_.maxHttpBufferSize;
transport.httpCompression = this_.httpCompression;
} else if ('websocket' === transportName) {
transport.perMessageDeflate = this_.perMessageDeflate;
}

try {
var transport = new transports[transportName](req);
if ('polling' === transportName) {
transport.maxHttpBufferSize = this.maxHttpBufferSize;
transport.httpCompression = this.httpCompression;
} else if ('websocket' === transportName) {
transport.perMessageDeflate = this.perMessageDeflate;
if (req._query && req._query.b64) {
transport.supportsBinary = false;
} else {
transport.supportsBinary = true;
}
} catch (e) {
sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
return;
}

if (req._query && req._query.b64) {
transport.supportsBinary = false;
} else {
transport.supportsBinary = true;
var socket = new Socket(id, this_, transport, req);

if (false !== this.cookie) {
transport.on('headers', function (headers) {
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
{
path: self.cookiePath,
httpOnly: self.cookiePath ? self.cookieHttpOnly : false
});
});
}
} catch (e) {
sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
return;
}
var socket = new Socket(id, this, transport, req);
var self = this;

if (false !== this.cookie) {
transport.on('headers', function (headers) {
headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
{
path: self.cookiePath,
httpOnly: self.cookiePath ? self.cookieHttpOnly : false
});
});
}
transport.onRequest(req);

transport.onRequest(req);
this.clients[id] = socket;
this.clientsCount++;

this.clients[id] = socket;
this.clientsCount++;
socket.once('close', function () {
delete self.clients[id];
self.clientsCount--;
});

socket.once('close', function () {
delete self.clients[id];
self.clientsCount--;
this.emit('connection', socket);
});

this.emit('connection', socket);
};

/**
Expand Down