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

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

View File

@ -9,19 +9,19 @@ const DAD = (module.exports = class RpcSocket extends ws {
DAD.upgrade(ws)
}
static upgrade(ws) {
static upgrade (ws) {
ws.__proto__ = DAD.prototype
ws.sid = randomBytes(16).toString('hex')
ws.onmessage = async ({ data }) => {
// console.log('[onmessage] 被调用在 data =', data)
// console.log({ _at: new Date().toJSON(), about:'[onmessage] 被调用在 data =', data})
try {
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) {
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)
else rpcResult = { _state: 'ERROR', _stateMsg: `unknown rpcHow=${rpcHow}` }
if (randomEvent) {
@ -30,20 +30,20 @@ const DAD = (module.exports = class RpcSocket extends ws {
break
case 'SEND_RESULT':
// 接收到远程返回的结果
// console.log('主调方 收到 SEND_RESULT: rpcResult=', rpcResult)
// console.log({ _at: new Date().toJSON(),about:'主调方 收到 SEND_RESULT', rpcResult})
ws.emit(randomEvent, rpcResult)
break
case 'SEND_NOTIFY':
default:
// 接收到同步的远程调用 或者 标准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)
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
}
} catch (exception) {
console.error('[onmessage] invalid rpc data: ', data, exception)
console.error({ _at: new Date().toJSON(), about: '[onmessage] invalid rpc data', data, exception })
return
}
}
@ -51,7 +51,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
return ws
}
static connectAsync(url) {
static connectAsync (url) {
return new Promise(function (resolve, reject) {
let socket = new DAD(url)
socket.onopen = () => resolve(socket)
@ -59,7 +59,7 @@ const DAD = (module.exports = class RpcSocket extends ws {
})
}
reconnectAsync(url) {
reconnectAsync (url) {
if (this.readyState === ws.CONNECTING || this.readyState === ws.OPEN) {
return Promise.resolve(this)
}
@ -70,19 +70,19 @@ const DAD = (module.exports = class RpcSocket extends ws {
})
}
sendNotify({ rpcHow, rpcWhat, options, callback } = {}) {
sendNotify ({ rpcHow, rpcWhat, options, callback } = {}) {
// 发起同步的远程调用
this.send(JSON.stringify({ rpcType: 'SEND_NOTIFY', rpcHow, rpcWhat }), options, callback)
}
sendRequest({ rpcHow, rpcWhat, rpcCallback, timeout = 5000 } = {}) {
sendRequest ({ rpcHow, rpcWhat, rpcCallback, timeout = 5000 } = {}) {
// 发起异步的远程调用
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') {
// 有回调
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)
setTimeout(() => {
if (this.eventNames().indexOf(randomEvent) >= 0) {