Comparing version 0.0.7 to 0.0.8
@@ -41,3 +41,2 @@ 'use strict'; | ||
this.socket = dgram.createSocket('udp4'); | ||
@@ -47,18 +46,50 @@ this.ShortName = 'Art-Net NodeJs'; | ||
this.nodes = {}; | ||
} | ||
_initSocket() { | ||
this.socket = dgram.createSocket('udp4'); | ||
this.socket.on('message', (msg, peer) => this._processMsg(msg, peer)); | ||
this.socket.on('error', e => console.log(e)); | ||
this.nodes = {}; | ||
} | ||
start() { | ||
this.socket.bind(PORT, () => { | ||
async start(config) { | ||
if(this.socket) { | ||
await this.stop(); | ||
} | ||
this._initSocket(); | ||
let readyPromise = new Promise(resolve => this.socket.once('listening', () => resolve())); | ||
this.socket.bind({ | ||
port: PORT, | ||
address: config.address | ||
}, () => { | ||
this.socket.setBroadcast(true); | ||
}); | ||
setInterval(() => this._sendPoll(), 1000); | ||
await readyPromise; | ||
clearInterval(this._pollTimer); | ||
this._pollTimer = setInterval(() => this._sendPoll(), 1000); | ||
} | ||
async stop() { | ||
clearInterval(this._pollTimer); | ||
if(this.socket) { | ||
await new Promise(resolve => { | ||
try { | ||
this.socket.close(() => resolve()); | ||
} catch(e) {} | ||
delete this.socket; | ||
}); | ||
} | ||
this.nodes = {}; | ||
this.emit('nodes', Object.keys(this.nodes).map(k => this.nodes[k])); | ||
} | ||
_processMsg(msg, peer) { | ||
@@ -65,0 +96,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"author": "Christian Blaschke <mail@platdesign.de>", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"dependencies": { | ||
@@ -8,0 +8,0 @@ "bipro": "0.0.8", |
366945
1279