socketcluster-client
Advanced tools
Comparing version 11.0.2 to 11.1.0
{ | ||
"name": "socketcluster-client", | ||
"main": "socketcluster.js", | ||
"version": "11.0.2", | ||
"version": "11.1.0", | ||
"homepage": "https://github.com/SocketCluster/socketcluster-client", | ||
@@ -6,0 +6,0 @@ "description": "SocketCluster JavaScript client", |
@@ -21,2 +21,2 @@ var SCSocket = require('./lib/scsocket'); | ||
module.exports.version = '11.0.2'; | ||
module.exports.version = '11.1.0'; |
@@ -141,14 +141,12 @@ var Emitter = require('component-emitter'); | ||
if (this.options.autoConnect) { | ||
this.connect(); | ||
} | ||
this._channelEmitter = new Emitter(); | ||
if (isBrowser && this.disconnectOnUnload && global.addEventListener) { | ||
this._unloadHandler = function () { | ||
self.disconnect(); | ||
}; | ||
this._unloadHandler = function () { | ||
self.disconnect(); | ||
}; | ||
global.addEventListener('beforeunload', this._unloadHandler, false); | ||
if (this.options.autoConnect) { | ||
this.connect(); | ||
} else { | ||
this.activate(); | ||
} | ||
@@ -260,2 +258,4 @@ }; | ||
this.activate(); | ||
if (this.state == this.CLOSED) { | ||
@@ -321,6 +321,15 @@ this.pendingReconnect = false; | ||
SCSocket.prototype.activate = function () { | ||
if (isBrowser && this.disconnectOnUnload && global.addEventListener && !this.active) { | ||
global.addEventListener('beforeunload', this._unloadHandler, false); | ||
} | ||
this._clientMap[this.clientId] = this; | ||
this.active = true; | ||
}; | ||
SCSocket.prototype.destroy = function (code, data) { | ||
if (this._unloadHandler) { | ||
if (isBrowser && global.removeEventListener) { | ||
global.removeEventListener('beforeunload', this._unloadHandler, false); | ||
} | ||
this.active = false; | ||
this.disconnect(code, data); | ||
@@ -530,3 +539,5 @@ delete this._clientMap[this.clientId]; | ||
this._flushEmitBuffer(); | ||
if (this.state == this.OPEN) { | ||
this._flushEmitBuffer(); | ||
} | ||
}; | ||
@@ -724,3 +735,2 @@ | ||
this._emitBuffer.append(eventNode); | ||
if (this.state == this.OPEN) { | ||
@@ -727,0 +737,0 @@ this._flushEmitBuffer(); |
@@ -25,2 +25,4 @@ var Emitter = require('component-emitter'); | ||
var SCTransport = function (authEngine, codecEngine, options) { | ||
var self = this; | ||
this.state = this.CLOSED; | ||
@@ -39,44 +41,4 @@ this.auth = authEngine; | ||
this.open(); | ||
}; | ||
// Open the connection. | ||
SCTransport.prototype = Object.create(Emitter.prototype); | ||
SCTransport.CONNECTING = SCTransport.prototype.CONNECTING = 'connecting'; | ||
SCTransport.OPEN = SCTransport.prototype.OPEN = 'open'; | ||
SCTransport.CLOSED = SCTransport.prototype.CLOSED = 'closed'; | ||
SCTransport.prototype.uri = function () { | ||
var query = this.options.query || {}; | ||
var schema = this.options.secure ? 'wss' : 'ws'; | ||
if (this.options.timestampRequests) { | ||
query[this.options.timestampParam] = (new Date()).getTime(); | ||
} | ||
query = querystring.encode(query); | ||
if (query.length) { | ||
query = '?' + query; | ||
} | ||
var host; | ||
if (this.options.host) { | ||
host = this.options.host; | ||
} else { | ||
var port = ''; | ||
if (this.options.port && ((schema == 'wss' && this.options.port != 443) | ||
|| (schema == 'ws' && this.options.port != 80))) { | ||
port = ':' + this.options.port; | ||
} | ||
host = this.options.hostname + port; | ||
} | ||
return schema + '://' + host + this.options.path + query; | ||
}; | ||
SCTransport.prototype.open = function () { | ||
var self = this; | ||
this.state = this.CONNECTING; | ||
@@ -87,2 +49,3 @@ var uri = this.uri(); | ||
wsSocket.binaryType = this.options.binaryType; | ||
this.socket = wsSocket; | ||
@@ -130,2 +93,38 @@ | ||
SCTransport.prototype = Object.create(Emitter.prototype); | ||
SCTransport.CONNECTING = SCTransport.prototype.CONNECTING = 'connecting'; | ||
SCTransport.OPEN = SCTransport.prototype.OPEN = 'open'; | ||
SCTransport.CLOSED = SCTransport.prototype.CLOSED = 'closed'; | ||
SCTransport.prototype.uri = function () { | ||
var query = this.options.query || {}; | ||
var schema = this.options.secure ? 'wss' : 'ws'; | ||
if (this.options.timestampRequests) { | ||
query[this.options.timestampParam] = (new Date()).getTime(); | ||
} | ||
query = querystring.encode(query); | ||
if (query.length) { | ||
query = '?' + query; | ||
} | ||
var host; | ||
if (this.options.host) { | ||
host = this.options.host; | ||
} else { | ||
var port = ''; | ||
if (this.options.port && ((schema == 'wss' && this.options.port != 443) | ||
|| (schema == 'ws' && this.options.port != 80))) { | ||
port = ':' + this.options.port; | ||
} | ||
host = this.options.hostname + port; | ||
} | ||
return schema + '://' + host + this.options.path + query; | ||
}; | ||
SCTransport.prototype._onOpen = function () { | ||
@@ -132,0 +131,0 @@ var self = this; |
{ | ||
"name": "socketcluster-client", | ||
"description": "SocketCluster JavaScript client", | ||
"version": "11.0.2", | ||
"version": "11.1.0", | ||
"homepage": "http://socketcluster.io", | ||
@@ -30,3 +30,3 @@ "contributors": [ | ||
"uuid": "3.2.1", | ||
"ws": "3.3.3" | ||
"ws": "5.1.1" | ||
}, | ||
@@ -52,3 +52,3 @@ "browser": { | ||
"mocha": "3.0.2", | ||
"socketcluster-server": "^11.0.0", | ||
"socketcluster-server": "^11.1.0", | ||
"uglify-js": "^2.7.5", | ||
@@ -55,0 +55,0 @@ "vinyl-source-stream": "^1.1.0" |
@@ -56,3 +56,3 @@ var assert = require('assert'); | ||
describe('integration tests', function () { | ||
before('run the server before start', function (done) { | ||
beforeEach('run the server before start', function (done) { | ||
server = socketClusterServer.listen(PORT, serverOptions); | ||
@@ -71,13 +71,2 @@ server.on('connection', connectionHandler); | ||
server.once('ready', function () { | ||
done(); | ||
}); | ||
}); | ||
after('shut down server afterwards', function (done) { | ||
server.close(); | ||
done(); | ||
}); | ||
beforeEach('Prepare data for test case', function (done) { | ||
clientOptions = { | ||
@@ -89,19 +78,31 @@ hostname: '127.0.0.1', | ||
}; | ||
done(); | ||
server.once('ready', function () { | ||
done(); | ||
}); | ||
}); | ||
afterEach('shut down client after each test', function (done) { | ||
afterEach('shut down server afterwards', function () { | ||
var cleanupTasks = []; | ||
global.localStorage.removeItem('socketCluster.authToken'); | ||
if (client && client.state != client.CLOSED) { | ||
client.once('disconnect', function () { | ||
done(); | ||
}); | ||
client.once('connectAbort', function () { | ||
done(); | ||
}); | ||
cleanupTasks.push(new Promise(function (resolve, reject) { | ||
client.once('disconnect', function () { | ||
resolve(); | ||
}); | ||
client.once('connectAbort', function () { | ||
resolve(); | ||
}); | ||
})); | ||
client.destroy(); | ||
} else { | ||
client.destroy(); | ||
done(); | ||
} | ||
cleanupTasks.push(new Promise(function (resolve) { | ||
server.close(function () { | ||
PORT++; | ||
resolve(); | ||
}); | ||
})); | ||
return Promise.all(cleanupTasks); | ||
}); | ||
@@ -189,3 +190,3 @@ | ||
it('token should be available inside login callback if token engine signing is synchronous', function (done) { | ||
var port = 8009; | ||
var port = 8509; | ||
server = socketClusterServer.listen(port, { | ||
@@ -214,3 +215,3 @@ authKey: serverOptions.authKey, | ||
it('if token engine signing is asynchronous, authentication can be captured using the authenticate event', function (done) { | ||
var port = 8010; | ||
var port = 8510; | ||
server = socketClusterServer.listen(port, { | ||
@@ -240,3 +241,3 @@ authKey: serverOptions.authKey, | ||
it('should still work if token verification is asynchronous', function (done) { | ||
var port = 8011; | ||
var port = 8511; | ||
server = socketClusterServer.listen(port, { | ||
@@ -902,3 +903,3 @@ authKey: serverOptions.authKey, | ||
describe('order of local events', function () { | ||
describe('order of events', function () { | ||
it('should trigger unsubscribe event on channel before disconnect event', function (done) { | ||
@@ -988,2 +989,170 @@ client = socketClusterClient.create(clientOptions); | ||
}); | ||
it('should reconnect if emit is called on a disconnected socket', function (done) { | ||
var fooEventTriggered = false; | ||
server.on('connection', function (socket) { | ||
socket.on('foo', function () { | ||
fooEventTriggered = true; | ||
}); | ||
}); | ||
client = socketClusterClient.create(clientOptions); | ||
var clientError; | ||
client.on('error', function (err) { | ||
clientError = err; | ||
}); | ||
var eventList = []; | ||
client.on('connecting', function () { | ||
eventList.push('connecting'); | ||
}); | ||
client.on('connect', function () { | ||
eventList.push('connect'); | ||
}); | ||
client.on('disconnect', function () { | ||
eventList.push('disconnect'); | ||
}); | ||
client.on('close', function () { | ||
eventList.push('close'); | ||
}); | ||
client.on('connectAbort', function () { | ||
eventList.push('connectAbort'); | ||
}); | ||
client.once('connect', function () { | ||
client.disconnect(); | ||
client.emit('foo', 123); | ||
}); | ||
setTimeout(function () { | ||
var expectedEventList = ['connect', 'disconnect', 'close', 'connecting', 'connect']; | ||
assert.equal(JSON.stringify(eventList), JSON.stringify(expectedEventList)); | ||
assert.equal(fooEventTriggered, true); | ||
done(); | ||
}, 1000); | ||
}); | ||
it('should reactivate and reconnect socket if emit is called on a destroyed socket', function (done) { | ||
client = socketClusterClient.create(clientOptions); | ||
assert.equal(client.active, true); | ||
var clientError; | ||
client.on('error', function (err) { | ||
clientError = err; | ||
}); | ||
client.once('connect', function () { | ||
assert.equal(client.active, true); | ||
client.destroy(); | ||
assert.equal(client.active, false); | ||
client.once('connect', function () { | ||
assert.equal(client.active, true); | ||
done(); | ||
}); | ||
client.emit('foo', 123); | ||
assert.equal(client.active, true); | ||
assert.equal(client.state, client.CONNECTING); | ||
}); | ||
}); | ||
it('should correctly handle multiple successive connect and disconnect calls', function (done) { | ||
client = socketClusterClient.create(clientOptions); | ||
var eventList = []; | ||
var clientError; | ||
client.on('error', function (err) { | ||
clientError = err; | ||
}); | ||
client.on('connecting', function () { | ||
eventList.push({ | ||
event: 'connecting' | ||
}); | ||
}); | ||
client.on('connect', function () { | ||
eventList.push({ | ||
event: 'connect' | ||
}); | ||
}); | ||
client.on('connectAbort', function (code, reason) { | ||
eventList.push({ | ||
event: 'connectAbort', | ||
code: code, | ||
reason: reason | ||
}); | ||
}); | ||
client.on('disconnect', function (code, reason) { | ||
eventList.push({ | ||
event: 'disconnect', | ||
code: code, | ||
reason: reason | ||
}); | ||
}); | ||
client.on('close', function (code, reason) { | ||
eventList.push({ | ||
event: 'close', | ||
code: code, | ||
reason: reason | ||
}); | ||
}); | ||
client.disconnect(1000, 'One'); | ||
client.connect(); | ||
client.disconnect(4444, 'Two'); | ||
client.once('connect', function () { | ||
client.disconnect(4455, 'Three'); | ||
}); | ||
client.connect(); | ||
setTimeout(function () { | ||
var expectedEventList = [ | ||
{ | ||
event: 'connectAbort', | ||
code: 1000, | ||
reason: 'One' | ||
}, | ||
{ | ||
event: 'close', | ||
code: 1000, | ||
reason: 'One' | ||
}, | ||
{ | ||
event: 'connecting' | ||
}, | ||
{ | ||
event: 'connectAbort', | ||
code: 4444, | ||
reason: 'Two' | ||
}, | ||
{ | ||
event: 'close', | ||
code: 4444, | ||
reason: 'Two' | ||
}, | ||
{ | ||
event: 'connecting' | ||
}, | ||
{ | ||
event: 'connect' | ||
}, | ||
{ | ||
event: 'disconnect', | ||
code: 4455, | ||
reason: 'Three' | ||
}, | ||
{ | ||
event: 'close', | ||
code: 4455, | ||
reason: 'Three' | ||
}, | ||
]; | ||
assert.equal(JSON.stringify(eventList), JSON.stringify(expectedEventList)); | ||
done(); | ||
}, 200); | ||
}); | ||
}); | ||
@@ -990,0 +1159,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
326796
7595
+ Addedws@5.1.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedultron@1.1.1(transitive)
- Removedws@3.3.3(transitive)
Updatedws@5.1.1