Skip to content

Commit 809e2bc

Browse files
committed
Merge pull request socketio#1529 from rase-/add/bc
Added socket.handshake BC object
2 parents 5bc75ea + 10adcb0 commit 809e2bc

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/socket.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
var Emitter = require('events').EventEmitter;
77
var parser = require('socket.io-parser');
8+
var url = require('url');
89
var debug = require('debug')('socket.io:socket');
910
var hasBin = require('has-binary-data');
1011

@@ -65,6 +66,7 @@ function Socket(nsp, client){
6566
this.acks = {};
6667
this.connected = true;
6768
this.disconnected = false;
69+
this.handshake = this.buildHandshake();
6870
}
6971

7072
/**
@@ -95,6 +97,25 @@ Socket.prototype.__defineGetter__('request', function(){
9597
return this.conn.request;
9698
});
9799

100+
/**
101+
* Builds the `handshake` BC object
102+
*
103+
* @api private
104+
*/
105+
106+
Socket.prototype.buildHandshake = function(){
107+
return {
108+
headers: this.request.headers,
109+
time: (new Date) + '',
110+
address: this.request.connection.address(),
111+
xdomain: !!this.request.headers.origin,
112+
secure: !!this.request.connection.encrypted,
113+
issued: +(new Date),
114+
url: this.request.url,
115+
query: url.parse(this.request.url, true).query || {}
116+
};
117+
};
118+
98119
/**
99120
* Emits to this client.
100121
*

test/socket.io.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,40 @@ describe('socket.io', function(){
9999
expect().fail();
100100
});
101101
});
102+
103+
it('should set the handshake BC object', function(done){
104+
var httpSrv = http();
105+
var srv = io(httpSrv);
106+
107+
srv.on('connection', function(s) {
108+
expect(s.handshake).to.not.be(undefined);
109+
110+
// Headers set and has some valid properties
111+
expect(s.handshake.headers).to.be.an('object');
112+
expect(s.handshake.headers['user-agent']).to.be('node-XMLHttpRequest');
113+
114+
// Time set and is valid looking string
115+
expect(s.handshake.time).to.be.a('string');
116+
expect(s.handshake.time.split(' ').length > 0); // Is "multipart" string representation
117+
118+
// Address, xdomain, secure, issued and url set
119+
expect(s.handshake.address).to.not.be(undefined);
120+
expect(s.handshake.xdomain).to.be.a('boolean');
121+
expect(s.handshake.secure).to.be.a('boolean');
122+
expect(s.handshake.issued).to.be.a('number');
123+
expect(s.handshake.url).to.be.a('string');
124+
125+
// Query set and has some right properties
126+
expect(s.handshake.query).to.be.an('object');
127+
expect(s.handshake.query.EIO).to.not.be(undefined);
128+
expect(s.handshake.query.transport).to.not.be(undefined);
129+
expect(s.handshake.query.t).to.not.be(undefined);
130+
131+
done();
132+
});
133+
134+
var socket = client(httpSrv);
135+
});
102136
});
103137

104138
describe('server attachment', function(){

0 commit comments

Comments
 (0)