Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplexed ws: Added support for proposed ping/pong message types. #282

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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