Comparing version 0.3.2 to 0.4.0
{ | ||
"name": "ataraxia", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "P2P messaging over mesh networks", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,2 +8,4 @@ 'use strict'; | ||
const CURRENT_VERSION = 2; | ||
module.exports = class Peer { | ||
@@ -15,2 +17,3 @@ constructor(transport) { | ||
this.events = new EventEmitter(); | ||
this.connected = false; | ||
@@ -20,3 +23,3 @@ // Reply to hello messages with our metadata | ||
this.id = msg.id; | ||
this.version = msg.version; | ||
this.version = Math.min(msg.version, CURRENT_VERSION); | ||
@@ -30,4 +33,23 @@ this.debug = debug(ns + ':' + msg.id); | ||
// Assume we are connected when hello is received | ||
this.events.emit('connected'); | ||
if(this.version >= 2) { | ||
// Setup a ping every 5 seconds | ||
const pingInterval = 5000; | ||
this.pingSender = setInterval(() => this.write('ping'), pingInterval); | ||
this.pingTimeout = setTimeout(() => this.socket.destroy(), pingInterval * 4); | ||
this.on('ping', () => { | ||
if(! this.connected) { | ||
// Consider the peer connected | ||
this.connected = true; | ||
this.events.emit('connected'); | ||
} | ||
clearTimeout(this.pingTimeout); | ||
this.pingTimeout = setTimeout(() => this.socket.destroy(), pingInterval * 4); | ||
}); | ||
this.write('ping'); | ||
} else { | ||
// Assume we are connected when hello is received | ||
this.events.emit('connected'); | ||
} | ||
}); | ||
@@ -72,2 +94,8 @@ } | ||
clearTimeout(this.helloTimeout); | ||
clearInterval(this.pingSender); | ||
clearTimeout(this.pingTimeout); | ||
this.connected = false; | ||
this.events.emit('disconnected'); | ||
@@ -80,3 +108,3 @@ } | ||
id: this.networkId, | ||
version: 1 | ||
version: CURRENT_VERSION | ||
}); | ||
@@ -83,0 +111,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17423
10
539