ipc: remove socket.io, rewrite ultra slim ipc
This commit is contained in:
parent
874d6f04f9
commit
73bc2e4187
11 changed files with 317 additions and 41 deletions
124
app/main/ipc/client.js
Normal file
124
app/main/ipc/client.js
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
var EventEmitter = require('events').EventEmitter
|
||||
|
||||
var hearbeatInterval = 25000,
|
||||
hearbeatTimeout = 5000,
|
||||
dieTimeout = 60000
|
||||
|
||||
class Socket extends EventEmitter {
|
||||
|
||||
constructor(ws, id) {
|
||||
|
||||
super()
|
||||
|
||||
this.id = id
|
||||
this.socket = null
|
||||
|
||||
this.queue = []
|
||||
|
||||
this.hearbeat = undefined
|
||||
this.hearbeatTimeout = undefined
|
||||
this.on('pong', ()=>{
|
||||
console.log('pong received')
|
||||
this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout)
|
||||
})
|
||||
this.on('ping', ()=>{
|
||||
this.socket.send(`["pong"]`)
|
||||
})
|
||||
|
||||
this.dieTimeout = undefined
|
||||
|
||||
this.open(ws)
|
||||
|
||||
}
|
||||
|
||||
open(ws) {
|
||||
|
||||
this.socket = ws
|
||||
|
||||
this.socket.onmessage = (e)=>{
|
||||
this.receive(e.data)
|
||||
}
|
||||
|
||||
this.socket.onclose = ()=>{
|
||||
console.log('close')
|
||||
this.close()
|
||||
}
|
||||
|
||||
this.socket.onerror = ()=>{
|
||||
console.log('error')
|
||||
this.close()
|
||||
}
|
||||
|
||||
this.dieTimeout = clearTimeout(this.dieTimeout)
|
||||
|
||||
this.hearbeat = setInterval(()=>{
|
||||
console.log('ping sent')
|
||||
this.socket.send(`["ping"]`)
|
||||
this.hearbeatTimeout = setTimeout(()=>{
|
||||
this.socket.close()
|
||||
}, hearbeatTimeout)
|
||||
}, hearbeatInterval)
|
||||
|
||||
}
|
||||
|
||||
close() {
|
||||
|
||||
this.hearbeat = clearInterval(this.hearbeat)
|
||||
|
||||
this.dieTimeout = setTimeout(()=>{
|
||||
|
||||
this.emit('close')
|
||||
|
||||
}, dieTimeout)
|
||||
|
||||
}
|
||||
|
||||
connected() {
|
||||
|
||||
return this.socket.readyState == this.socket.OPEN
|
||||
|
||||
}
|
||||
|
||||
receive(message) {
|
||||
|
||||
if (typeof message == 'string') {
|
||||
|
||||
var packet = JSON.parse(message)
|
||||
|
||||
if (Array.isArray(packet) && typeof packet[0] == "string") {
|
||||
|
||||
this.emit(packet[0], packet[1])
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
send(event, data) {
|
||||
|
||||
var packet = JSON.stringify([event, data])
|
||||
|
||||
if (this.connected()) {
|
||||
this.socket.send(packet)
|
||||
} else {
|
||||
this.queue.push(packet)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
flush(){
|
||||
|
||||
for (var i in this.queue) {
|
||||
|
||||
this.socket.send(this.queue[i])
|
||||
|
||||
}
|
||||
|
||||
this.queue = []
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Socket
|
||||
59
app/main/ipc/server.js
Normal file
59
app/main/ipc/server.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
var EventEmitter = require('events').EventEmitter,
|
||||
WebSocketServer = require('ws').Server,
|
||||
Client = require('./client')
|
||||
|
||||
class Ipc extends EventEmitter {
|
||||
|
||||
constructor(server) {
|
||||
|
||||
super()
|
||||
|
||||
this.clients = {}
|
||||
this.server = null
|
||||
|
||||
this.server = new WebSocketServer({server: server})
|
||||
|
||||
this.server.on('connection', (socket, req) => {
|
||||
|
||||
var id = req.url.split('/').pop()
|
||||
|
||||
if (!id) return
|
||||
|
||||
if (!this.clients[id]) {
|
||||
|
||||
var client = new Client(socket, id)
|
||||
this.clients[id] = client
|
||||
this.emit('connection', client)
|
||||
|
||||
client.on('close', ()=>{
|
||||
console.log('client died for real')
|
||||
delete this.clients[id]
|
||||
})
|
||||
|
||||
} else {
|
||||
|
||||
this.clients[id].open(socket)
|
||||
this.clients[id].flush()
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
send(event, data, id, except) {
|
||||
|
||||
var clients = id ? [this.clients[id]] : this.clients
|
||||
|
||||
for (var k in clients) {
|
||||
if (!except || this.clients[k] != this.clients[except]) {
|
||||
clients[k].send(event, data)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = Ipc
|
||||
Loading…
Add table
Add a link
Reference in a new issue