console.log({_at, ...})

This commit is contained in:
Luk 2024-02-05 09:50:01 +08:00
parent 599f656646
commit e0ba1e32be

View File

@ -14,14 +14,14 @@ const DAD = (module.exports = class RpcSocket extends ws {
ws.sid = randomBytes(16).toString('hex') ws.sid = randomBytes(16).toString('hex')
ws.onmessage = async ({ data }) => { ws.onmessage = async ({ data }) => {
// console.log('[onmessage] 被调用在 data =', data) // console.log({ _at: new Date().toJSON(), about:'[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 ', {rpcType, rpcHow, rpcWhat, randomEvent, rpcResult}) // console.log({ _at: new Date().toJSON(), about:'message data parsed', rpcType, rpcHow, rpcWhat, randomEvent, rpcResult})
switch (rpcType) { switch (rpcType) {
case 'SEND_REQUEST': case 'SEND_REQUEST':
// 接收到异步的远程调用 // 接收到异步的远程调用
// console.log(`被调方 收到 SEND_REQUEST: rpcHow=${rpcHow}, rpcWhat=${JSON.stringify(rpcWhat)}`) // console.log({ _at: new Date().toJSON(), about:'被调方 收到 SEND_REQUEST', rpcHow, rpcWhat})
if (ws.hasOwnProperty(rpcHow)) rpcResult = await ws[rpcHow](rpcWhat) if (ws.hasOwnProperty(rpcHow)) rpcResult = await ws[rpcHow](rpcWhat)
else rpcResult = { _state: 'ERROR', _stateMsg: `unknown rpcHow=${rpcHow}` } else rpcResult = { _state: 'ERROR', _stateMsg: `unknown rpcHow=${rpcHow}` }
if (randomEvent) { if (randomEvent) {
@ -30,20 +30,20 @@ const DAD = (module.exports = class RpcSocket extends ws {
break break
case 'SEND_RESULT': case 'SEND_RESULT':
// 接收到远程返回的结果 // 接收到远程返回的结果
// console.log('主调方 收到 SEND_RESULT: rpcResult=', rpcResult) // console.log({ _at: new Date().toJSON(),about:'主调方 收到 SEND_RESULT', 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_NOFITY: rpcHow=${rpcHow}, rpcWhat=${JSON.stringify(rpcWhat)}`) // console.log({ _at: new Date().toJSON(),about:'被调方 收到 SEND_NOFITY', rpcHow, 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.error('[onmessage] unknown rpc: ', rpcHow, rpcWhat) else console.error({ _at: new Date().toJSON(), about: '[onmessage] unknown rpc', rpcHow, rpcWhat })
break break
} }
} catch (exception) { } catch (exception) {
console.error('[onmessage] invalid rpc data: ', data, exception) console.error({ _at: new Date().toJSON(), about: '[onmessage] invalid rpc data', data, exception })
return return
} }
} }
@ -78,11 +78,11 @@ const DAD = (module.exports = class RpcSocket extends ws {
sendRequest ({ rpcHow, rpcWhat, rpcCallback, timeout = 5000 } = {}) { sendRequest ({ rpcHow, rpcWhat, rpcCallback, timeout = 5000 } = {}) {
// 发起异步的远程调用 // 发起异步的远程调用
let randomEvent = randomBytes(16).toString('hex') let randomEvent = randomBytes(16).toString('hex')
// console.log('randomEvent is randomized: ', randomEvent) // console.log({ _at: new Date().toJSON(),about:'randomEvent is randomized', randomEvent})
if (typeof rpcCallback === 'function') { if (typeof rpcCallback === 'function') {
// 有回调 // 有回调
this.send(JSON.stringify({ rpcType: 'SEND_REQUEST', rpcHow, rpcWhat, randomEvent }), () => { this.send(JSON.stringify({ rpcType: 'SEND_REQUEST', rpcHow, rpcWhat, randomEvent }), () => {
// console.log('in callback, randomEvent=', randomEvent) // console.log({ _at: new Date().toJSON(),about:'in callback', randomEvent})
this.once(randomEvent, rpcCallback) this.once(randomEvent, rpcCallback)
setTimeout(() => { setTimeout(() => {
if (this.eventNames().indexOf(randomEvent) >= 0) { if (this.eventNames().indexOf(randomEvent) >= 0) {