fix(p2p): p2p proper closing

This commit is contained in:
Alexey Kasyanchuk 2018-06-22 20:50:43 +03:00
parent ead88b0a03
commit 5efb309dc1
2 changed files with 12 additions and 1 deletions

View File

@ -12,6 +12,7 @@ class p2p {
{ {
this.events = new EventEmitter this.events = new EventEmitter
this.peers = [] this.peers = []
this.clients = []
this.ignoreAddresses = ['127.0.0.1'] this.ignoreAddresses = ['127.0.0.1']
this.messageHandlers = {} this.messageHandlers = {}
this.externalPeers = [] this.externalPeers = []
@ -34,6 +35,10 @@ class p2p {
console.log('server connected', con, 'max', this.tcpServer.maxConnections) console.log('server connected', con, 'max', this.tcpServer.maxConnections)
}) })
socket = new JsonSocket(socket); socket = new JsonSocket(socket);
this.clients.push(socket)
socket.on('close', () => {
this.clients.splice(this.clients.indexOf(socket), 1);
});
socket.on('error', (err) => {}) socket.on('error', (err) => {})
socket.on('message', (message) => { socket.on('message', (message) => {
if(message.type && this.messageHandlers[message.type]) if(message.type && this.messageHandlers[message.type])
@ -171,6 +176,12 @@ class p2p {
console.log('closing ssh...') console.log('closing ssh...')
this.ssh.kill() this.ssh.kill()
} }
// close server
const promise = new Promise(resolve => this.tcpServer.close(resolve))
for (const client in this.clients) {
this.clients[client]._socket.destroy();
}
return promise
} }
on(type, callback) { on(type, callback) {

View File

@ -767,7 +767,7 @@ setInterval(() => {
upnp.ratsUnmap() upnp.ratsUnmap()
console.log('closing p2p...') console.log('closing p2p...')
p2p.close() await p2p.close()
// save feed // save feed
await feed.save() await feed.save()