Skip to content

Commit

Permalink
Merge pull request #56 from PlayNetwork/rt221
Browse files Browse the repository at this point in the history
refresh token if expired on reconnect
  • Loading branch information
rmomii committed Nov 16, 2017
2 parents f61fa8d + d5264ce commit 6bd527b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
32 changes: 24 additions & 8 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ module.exports = function (playerOptions, ensureAuthHeaders, self) {
authHeaders = yield ensureAuthHeaders(),
authToken = authHeaders[AUTH_TOKEN],
clientId = authHeaders[CLIENT_ID],
query,
url;

url = [protocol,
'://',
settings.host,
'?clientId=',
clientId,
'&token=',
authToken].join('');
url = [protocol, '://', settings.host].join('');
query = ['clientId=', clientId, '&', 'token=', authToken].join('');

socket = io(url);
socket = new io(url, { 'query' : query});

// fired after a successful connection
socket.on('connect', () => {
Expand Down Expand Up @@ -113,12 +109,32 @@ module.exports = function (playerOptions, ensureAuthHeaders, self) {
socket.on('reconnecting', (attempt) => {
let connection = {
'connectionAttempt' : attempt,
'headerReset' : false,
'url' : url
};

socketEventSubscriber.emit('reconnecting', connection);
});

socket.on('reconnect_attempt', (attempt) => {
co(function *() {
let
authHeaders = yield ensureAuthHeaders(),
authToken = authHeaders[AUTH_TOKEN],
clientId = authHeaders[CLIENT_ID];

socket.io.opts.query = ['clientId=', clientId, '&', 'token=', authToken].join('');

let connection = {
'connectionAttempt' : attempt,
'headerReset' : true,
'url' : url
};

socketEventSubscriber.emit('reconnecting', connection);
});
});

// fired on reconnect error
socket.on('reconnect_error', (err) => {
socketEventSubscriber.emit('error', err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playnetwork-sdk",
"version": "1.3.2",
"version": "1.3.3",
"contributors": [
{
"name": "Joshua Thomas",
Expand Down
17 changes: 13 additions & 4 deletions test/Mocks/mockSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const events = require('events');
module.exports = function (opts, self) {
self = self ? self : {};

let configOpts = opts ? opts : {
notifySubscriber : {}
};
let
configOpts = opts ? opts : {
notifySubscriber : {}
};

function MockSocket() {
Object.create(this);
Expand Down Expand Up @@ -42,7 +43,12 @@ module.exports = function (opts, self) {

if (configOpts.notifySubscriber.reconnecting) {
setTimeout(function() {
MockSocket.prototype.emit.call(self, 'reconnecting', configOpts.notifySubscriber.reconnecting.number);
if (configOpts.notifySubscriber.reconnecting.headerReset) {
MockSocket.prototype.emit.call(self, 'reconnect_attempt', configOpts.notifySubscriber.reconnecting.number);
} else {
MockSocket.prototype.emit.call(self, 'reconnecting', configOpts.notifySubscriber.reconnecting.number);
}

}, configOpts.notifySubscriber.reconnecting.occursAt);
}

Expand All @@ -60,5 +66,8 @@ module.exports = function (opts, self) {
};

self = new MockSocket(opts);

self.io = { 'opts': { 'query' : {} } };

return self;
};
14 changes: 10 additions & 4 deletions test/lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,31 @@ describe('player', () => {
return done();
}

return done('Unexpected number arg expecting 5, actual ', number);
return done('Unexpected number arg expecting 5, actual ', connection.connectionAttempt);
});

player.connect(playerSubscriber);
}).timeout(5000);

it('#notify subscriber reconnecting', (done) => {
it('#notify subscriber reconnecting headerReset', (done) => {
let configOpts = {
notifySubscriber : {
reconnecting : {
headerReset : true,
number : 1,
occursAt : 2000
}
}
};

player = mockSocketIOClient.rewire('../../lib/player.js', configOpts)(null, ensureAuthHeaders);

playerSubscriber.on('reconnecting', () => {
return done();
playerSubscriber.on('reconnecting', (connection) => {
if (connection.headerReset) {
return done();
}

return done('Expected headerReset to be true, actual ', connection.headerReset);
});

player.connect(playerSubscriber);
Expand Down

0 comments on commit 6bd527b

Please sign in to comment.