优化
This commit is contained in:
parent
0177a27b33
commit
a110466245
22
index.js
22
index.js
@ -13,38 +13,38 @@ const DAD = (module.exports = class RpcSocket extends ws {
|
||||
static upgrade(ws) {
|
||||
ws.__proto__ = DAD.prototype
|
||||
ws.sid = randomBytes(16).toString('hex')
|
||||
|
||||
ws.onmessage = async ({ data }) => {
|
||||
// console.log('onmessage 被调用')
|
||||
// console.log('[onmessage] 被调用在 data =', data)
|
||||
try {
|
||||
let { rpcType, rpcHow, rpcWhat, randomEvent, rpcResult } = JSON.parse(data)
|
||||
// console.log('message data parsed to obj=',obj)
|
||||
// console.log('message data parsed to ', {rpcType, rpcHow, rpcWhat, randomEvent, rpcResult})
|
||||
switch (rpcType) {
|
||||
case 'SEND_REQUEST':
|
||||
// 接收到异步的远程调用
|
||||
// console.log(`被动方 收到 SEND_REQUEST_ASYNC: rpcHow=${obj.rpcHow}, rpcWhat=${JSON.stringify(obj.rpcWhat)}`)
|
||||
let rpcResult
|
||||
// console.log(`被调方 收到 SEND_REQUEST: rpcHow=${rpcHow}, rpcWhat=${JSON.stringify(rpcWhat)}`)
|
||||
if (ws.hasOwnProperty(rpcHow)) rpcResult = await ws[rpcHow](rpcWhat)
|
||||
else rpcResult = { _state: 'error', error: 'unknown rpcHow' }
|
||||
else rpcResult = { _state: 'ERROR', _stateMsg: `unknown rpcHow=${rpcHow}` }
|
||||
if (randomEvent) {
|
||||
ws.send(JSON.stringify({ rpcType: 'SEND_RESULT', randomEvent, rpcResult })) // 返回结果给远程
|
||||
}
|
||||
break
|
||||
case 'SEND_RESULT':
|
||||
// 接收到远程返回的结果
|
||||
// console.log('主动方 收到 SEND_RESULT: rpcResult=', obj.rpcResult)
|
||||
// console.log('主调方 收到 SEND_RESULT: rpcResult=', 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)}`)
|
||||
// console.log(`被调方 收到 SEND_NOFITY: rpcHow=${rpcHow}, rpcWhat=${JSON.stringify(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)
|
||||
else console.error('[onmessage] unknown rpc: ', rpcHow, rpcWhat)
|
||||
break
|
||||
}
|
||||
} catch (exception) {
|
||||
console.error('[onmessage] invalid rpc data: ', data)
|
||||
console.error('[onmessage] invalid rpc data: ', data, exception)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -88,7 +88,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
|
||||
setTimeout(() => {
|
||||
if (this.eventNames().indexOf(randomEvent) >= 0) {
|
||||
this.removeAllListeners(randomEvent)
|
||||
rpcCallback({ _state: 'RPC_REQUEST_TIMEOUT', message: `RpcSocket sendRequest timeout: ${rpcHow} ${rpcWhat}` })
|
||||
rpcCallback({ _state: 'RPC_REQUEST_TIMEOUT', _stateMsg: `RpcSocket sendRequest timeout: ${rpcHow} ${rpcWhat}` })
|
||||
}
|
||||
}, timeout)
|
||||
})
|
||||
@ -100,7 +100,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
|
||||
setTimeout(() => {
|
||||
if (this.eventNames().indexOf(randomEvent) >= 0) {
|
||||
this.removeAllListeners(randomEvent)
|
||||
resolve({ _state: 'RPC_REQUEST_TIMEOUT', error: `RpcSocket sendRequest timeout: ${rpcHow} ${rpcWhat}` })
|
||||
resolve({ _state: 'RPC_REQUEST_TIMEOUT', _stateMsg: `RpcSocket sendRequest timeout: ${rpcHow} ${rpcWhat}` })
|
||||
}
|
||||
}, timeout)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user