improvements

This commit is contained in:
陆柯 2021-03-23 20:36:35 +08:00
parent 900e7d54fa
commit 0177a27b33

View File

@ -19,12 +19,8 @@ const DAD = (module.exports = class RpcSocket extends ws {
let { rpcType, rpcHow, rpcWhat, randomEvent, rpcResult } = JSON.parse(data)
// console.log('message data parsed to obj=',obj)
switch (rpcType) {
case 'SEND_NOTIFY': // 接收到同步的远程调用
// console.log(`SEND_REQUEST_SYNC message: rpcHow=${obj.rpcHow}, rpcWhat=${JSON.stringify(obj.rpcWhat)}`)
if (ws.hasOwnProperty(rpcHow)) ws[rpcHow](rpcWhat)
else if (ws.eventNames().indexOf(rpcHow) >= 0) ws.emit(rpcHow, rpcWhat)
else console.log('onmessage unknown notification')
case 'SEND_REQUEST': // 接收到异步的远程调用
case 'SEND_REQUEST':
// 接收到异步的远程调用
// console.log(`被动方 收到 SEND_REQUEST_ASYNC: rpcHow=${obj.rpcHow}, rpcWhat=${JSON.stringify(obj.rpcWhat)}`)
let rpcResult
if (ws.hasOwnProperty(rpcHow)) rpcResult = await ws[rpcHow](rpcWhat)
@ -32,13 +28,23 @@ const DAD = (module.exports = class RpcSocket extends ws {
if (randomEvent) {
ws.send(JSON.stringify({ rpcType: 'SEND_RESULT', randomEvent, rpcResult })) // 返回结果给远程
}
case 'SEND_RESULT': // 接收到远程返回的结果
break
case 'SEND_RESULT':
// 接收到远程返回的结果
// console.log('主动方 收到 SEND_RESULT: rpcResult=', obj.rpcResult)
ws.emit(randomEvent, rpcResult)
break
case 'SEND_NOTIFY':
default:
// 接收到同步的远程调用 或者 标准ws的send(...)
// console.log(`SEND_REQUEST_SYNC message: rpcHow=${obj.rpcHow}, rpcWhat=${JSON.stringify(obj.rpcWhat)}`)
if (ws.hasOwnProperty(rpcHow)) ws[rpcHow](rpcWhat)
else if (ws.eventNames().indexOf(rpcHow) >= 0) ws.emit(rpcHow, rpcWhat)
else console.log('[onmessage] unknown rpc: ', rpcHow, rpcWhat)
break
}
} catch (exception) {
console.error('Invalid socket message received!')
console.error('[onmessage] invalid rpc data: ', data)
return
}
}
@ -65,24 +71,24 @@ const DAD = (module.exports = class RpcSocket extends ws {
})
}
sendNotify({ rpcHow, rpcWhat, options, cb } = {}) {
sendNotify({ rpcHow, rpcWhat, options, callback } = {}) {
// 发起同步的远程调用
this.send(JSON.stringify({ rpcType: 'SEND_NOTIFY', rpcHow, rpcWhat }), options, cb)
this.send(JSON.stringify({ rpcType: 'SEND_NOTIFY', rpcHow, rpcWhat }), options, callback)
}
sendRequest({ rpcHow, rpcWhat, ack, timeout = 5000 } = {}) {
sendRequest({ rpcHow, rpcWhat, rpcCallback, timeout = 5000 } = {}) {
// 发起异步的远程调用
let randomEvent = randomBytes(16).toString('hex')
// console.log('randomEvent is randomized: ', randomEvent)
if (typeof ack === 'function') {
if (typeof rpcCallback === 'function') {
// 有回调
this.send(JSON.stringify({ rpcType: 'SEND_REQUEST', rpcHow, rpcWhat, randomEvent }), () => {
// console.log('in callback, randomEvent=', randomEvent)
this.once(randomEvent, ack)
this.once(randomEvent, rpcCallback)
setTimeout(() => {
if (this.eventNames().indexOf(randomEvent) >= 0) {
this.removeAllListeners(randomEvent)
ack({ _state: 'timeout', error: `RpcSocket sendRequest ${rpcHow} timeout` })
rpcCallback({ _state: 'RPC_REQUEST_TIMEOUT', message: `RpcSocket sendRequest timeout: ${rpcHow} ${rpcWhat}` })
}
}, timeout)
})
@ -94,7 +100,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
setTimeout(() => {
if (this.eventNames().indexOf(randomEvent) >= 0) {
this.removeAllListeners(randomEvent)
resolve({ _state: 'timeout', error: `RpcSocket sendRequest ${rpcHow} timeout` })
resolve({ _state: 'RPC_REQUEST_TIMEOUT', error: `RpcSocket sendRequest timeout: ${rpcHow} ${rpcWhat}` })
}
}, timeout)
})