Skip to content

Commit 589d60a

Browse files
committed
Commented Connection's ping function
1 parent aaeb199 commit 589d60a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

connection.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ module.exports = class Connection {
224224
// increment connection counter by 1
225225
self.connection_progress_obj.counter++;
226226

227+
// start heartbeat to keep connection alive
227228
self.ping();
228229

229230
/**
@@ -304,10 +305,22 @@ module.exports = class Connection {
304305
});
305306
}
306307

308+
/**
309+
* Pings the server at a regular interval.
310+
* Websockets require a "heartbeat" in order to keep the conneciton open.
311+
* @returns {void}
312+
*/
307313
ping(){
314+
315+
// allows this to be used inside nested functions
308316
let self = this
317+
318+
// send a request to the websocket server ever 5 seconds
309319
this.pingTimer = setInterval(function () {
320+
310321
// create a JSON string containing the current request number
322+
// we use 0 as to not interer with any unsigned ints on the server end, as well as any possible
323+
// pening responses from the server
311324
let data = JSON.stringify({'c': 0});
312325

313326
// send the request to the websocket server
@@ -318,6 +331,7 @@ module.exports = class Connection {
318331

319332
/**
320333
* Closes the connection to the websocket server
334+
* @returns {void}
321335
*/
322336
close(){
323337
this.keep_alive = false;

0 commit comments

Comments
 (0)