Skip to content

Commit 85cb659

Browse files
committed
Merge pull request #250 from imkira/allowCustomSessionIds
Allow custom session ids
2 parents 7693d42 + e668284 commit 85cb659

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ Where `options` is a hash which can contain:
127127
option allows you to supply a list transports that may be used by
128128
SockJS. By default all available transports will be used.
129129

130+
* **sessionId (number OR function)**
131+
132+
Both client and server use session identifiers to distinguish connections.
133+
If you specify this option as a number, SockJS will use its random string
134+
generator function to generate session ids that are N-character long
135+
(where N corresponds to the number specified by **sessionId**).
136+
When you specify this option as a function, the function must return a
137+
randomly generated string. Every time SockJS needs to generate a session
138+
id it will call this function and use the returned string directly.
139+
If you don't specify this option, the default is to use the default random
140+
string generator to generate 8-character long session ids.
130141

131142
Although the 'SockJS' object tries to emulate the 'WebSocket'
132143
behaviour, it's impossible to support all of its features. An

lib/main.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ function SockJS(url, protocols, options) {
5050
log.warn("'protocols_whitelist' is DEPRECATED. Use 'transports' instead.");
5151
}
5252
this._transportsWhitelist = options.transports;
53+
54+
var sessionId = options.sessionId || 8;
55+
if (typeof sessionId === 'function') {
56+
this._generateSessionId = sessionId;
57+
} else if (typeof sessionId === 'number') {
58+
this._generateSessionId = function() {
59+
return random.string(sessionId);
60+
}
61+
} else {
62+
throw new TypeError("If sessionId is used in the options, it needs to be a number or a function.");
63+
}
64+
5365
this._server = options.server || random.numberString(1000);
5466

5567
// Step 1 of WS spec - parse and validate the url. Issue #8
@@ -202,7 +214,7 @@ SockJS.prototype._connect = function() {
202214
this._transportTimeoutId = setTimeout(this._transportTimeout.bind(this), timeoutMs);
203215
debug('using timeout', timeoutMs);
204216

205-
var transportUrl = urlUtils.addPath(this._transUrl, '/' + this._server + '/' + random.string(8));
217+
var transportUrl = urlUtils.addPath(this._transUrl, '/' + this._server + '/' + this._generateSessionId());
206218
debug('transport url', transportUrl);
207219
var transportObj = new Transport(transportUrl, this._transUrl);
208220
transportObj.on('message', this._transportMessage.bind(this));

tests/lib/main.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,36 @@ describe('SockJS', function() {
7070
});
7171
});
7272
});
73+
74+
it('should generate 8-character-long session ids by default', function () {
75+
var s = SockJS('http://localhost');
76+
expect(s._generateSessionId().length).to.be(8);
77+
s.close();
78+
});
79+
80+
it('should generate N-character-long session ids', function () {
81+
for (var i = 1; i <= 100; i++) {
82+
var s = SockJS('http://localhost', null, {sessionId: i});
83+
expect(s._generateSessionId().length).to.be(i);
84+
s.close();
85+
}
86+
});
87+
88+
it('should generate sessionIds using the custom generator function', function () {
89+
var f = function() {
90+
return 'this_is_not_random';
91+
};
92+
var s = SockJS('http://localhost', null, {sessionId: f});
93+
expect(s._generateSessionId).to.be(f);
94+
s.close();
95+
});
96+
97+
it('should throw TypeError if sessionId is neither a number nor a function', function () {
98+
expect(function () {
99+
new SockJS('http://localhost', null, {sessionId: 'this is wrong'});
100+
}).to.throwException(function (e) {
101+
expect(e).to.be.a(TypeError);
102+
});
103+
});
73104
});
74105
});

0 commit comments

Comments
 (0)