Skip to content

Commit

Permalink
Merge pull request #282 from zettajs/multiplexed-ws-add-ping-pong
Browse files Browse the repository at this point in the history
Multiplexed ws: Added support for proposed ping/pong message types.
  • Loading branch information
AdamMagaluk committed Jan 15, 2016
2 parents 4217a12 + 48053db commit f552c19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/event_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ var EventSocket = module.exports = function(ws, query, streamEnabled) {
self.ws.send(JSON.stringify(msg));
});

this._parser.on('ping', function(msg) {
var msg = {
type: 'pong',
timestamp: new Date().getTime(),
data: msg.data
};
self.ws.send(JSON.stringify(msg));
});

this._parser.on('subscribe', function(msg) {
var topic = new StreamTopic();
try {
Expand Down
34 changes: 34 additions & 0 deletions test/test_event_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,40 @@ describe('Event Streams', function() {
ws.on('error', done);
});

itBoth('sending ping request will return a pong response without data field', function(idx, done) {
var endpoint = urls[idx];
var ws = new WebSocket('ws://' + endpoint + baseUrl);
ws.on('open', function() {
var msg = { type: 'ping'};
ws.send(JSON.stringify(msg));
ws.on('message', function(buffer) {
var json = JSON.parse(buffer);
assert.equal(json.type, 'pong');
assert(json.timestamp);
assert.equal(json.data, undefined);
done();
});
});
ws.on('error', done);
});

itBoth('sending ping request will return a pong response with data field', function(idx, done) {
var endpoint = urls[idx];
var ws = new WebSocket('ws://' + endpoint + baseUrl);
ws.on('open', function() {
var msg = { type: 'ping', data: 'Application data'};
ws.send(JSON.stringify(msg));
ws.on('message', function(buffer) {
var json = JSON.parse(buffer);
assert.equal(json.type, 'pong');
assert(json.timestamp);
assert.equal(json.data, 'Application data');
done();
});
});
ws.on('error', done);
});

itBoth('unsubscribing to a topic receives a unsubscription-ack', function(idx, done) {
var endpoint = urls[idx];
var ws = new WebSocket('ws://' + endpoint + baseUrl);
Expand Down

0 comments on commit f552c19

Please sign in to comment.