This commit is contained in:
陆柯 2021-03-23 21:38:27 +08:00
parent 0177a27b33
commit a110466245

View File

@ -13,38 +13,38 @@ const DAD = (module.exports = class RpcSocket extends ws {
static upgrade(ws) { static upgrade(ws) {
ws.__proto__ = DAD.prototype ws.__proto__ = DAD.prototype
ws.sid = randomBytes(16).toString('hex') ws.sid = randomBytes(16).toString('hex')
ws.onmessage = async ({ data }) => { ws.onmessage = async ({ data }) => {
// console.log('onmessage 被调用') // console.log('[onmessage] 被调用在 data =', data)
try { try {
let { rpcType, rpcHow, rpcWhat, randomEvent, rpcResult } = JSON.parse(data) 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) { switch (rpcType) {
case 'SEND_REQUEST': case 'SEND_REQUEST':
// 接收到异步的远程调用 // 接收到异步的远程调用
// console.log(`被动方 收到 SEND_REQUEST_ASYNC: rpcHow=${obj.rpcHow}, rpcWhat=${JSON.stringify(obj.rpcWhat)}`) // console.log(`被调方 收到 SEND_REQUEST: rpcHow=${rpcHow}, rpcWhat=${JSON.stringify(rpcWhat)}`)
let rpcResult
if (ws.hasOwnProperty(rpcHow)) rpcResult = await ws[rpcHow](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) { if (randomEvent) {
ws.send(JSON.stringify({ rpcType: 'SEND_RESULT', randomEvent, rpcResult })) // 返回结果给远程 ws.send(JSON.stringify({ rpcType: 'SEND_RESULT', randomEvent, rpcResult })) // 返回结果给远程
} }
break break
case 'SEND_RESULT': case 'SEND_RESULT':
// 接收到远程返回的结果 // 接收到远程返回的结果
// console.log('主动方 收到 SEND_RESULT: rpcResult=', obj.rpcResult) // console.log('主调方 收到 SEND_RESULT: rpcResult=', rpcResult)
ws.emit(randomEvent, rpcResult) ws.emit(randomEvent, rpcResult)
break break
case 'SEND_NOTIFY': case 'SEND_NOTIFY':
default: default:
// 接收到同步的远程调用 或者 标准ws的send(...) // 接收到同步的远程调用 或者 标准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) if (ws.hasOwnProperty(rpcHow)) ws[rpcHow](rpcWhat)
else if (ws.eventNames().indexOf(rpcHow) >= 0) ws.emit(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 break
} }
} catch (exception) { } catch (exception) {
console.error('[onmessage] invalid rpc data: ', data) console.error('[onmessage] invalid rpc data: ', data, exception)
return return
} }
} }
@ -88,7 +88,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
setTimeout(() => { setTimeout(() => {
if (this.eventNames().indexOf(randomEvent) >= 0) { if (this.eventNames().indexOf(randomEvent) >= 0) {
this.removeAllListeners(randomEvent) 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) }, timeout)
}) })
@ -100,7 +100,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
setTimeout(() => { setTimeout(() => {
if (this.eventNames().indexOf(randomEvent) >= 0) { if (this.eventNames().indexOf(randomEvent) >= 0) {
this.removeAllListeners(randomEvent) 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) }, timeout)
}) })