From a1104662455845a2b01202456a636d2f1da615ed Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Tue, 23 Mar 2021 21:38:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 1d04ff9..b11f7f3 100644 --- a/index.js +++ b/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) })