diff --git a/lib/event_socket.js b/lib/event_socket.js index ba3e775..0e95e2b 100644 --- a/lib/event_socket.js +++ b/lib/event_socket.js @@ -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 { diff --git a/test/test_event_streams.js b/test/test_event_streams.js index 338f925..3360bc5 100644 --- a/test/test_event_streams.js +++ b/test/test_event_streams.js @@ -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);