u
This commit is contained in:
20
rpcsocket.js
20
rpcsocket.js
@@ -4,7 +4,7 @@ const { randomBytes } = require('crypto')
|
|||||||
// 建议把 onopen, onerror, onclose, onmessage 保留给系统使用,例如reconnecting。用户使用 on(event, listener)
|
// 建议把 onopen, onerror, onclose, onmessage 保留给系统使用,例如reconnecting。用户使用 on(event, listener)
|
||||||
|
|
||||||
const DAD = (module.exports = class RpcSocket extends ws {
|
const DAD = (module.exports = class RpcSocket extends ws {
|
||||||
constructor(address, protocols, options) {
|
constructor (address, protocols, options) {
|
||||||
let ws = super(address, protocols, options)
|
let ws = super(address, protocols, options)
|
||||||
DAD.upgrade(ws)
|
DAD.upgrade(ws)
|
||||||
}
|
}
|
||||||
@@ -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({ _at: new Date().toJSON(), about:'[onmessage] 被调用在 data =', data}, '\n,')
|
// globalThis.wo?.cclog?.({ 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({ _at: new Date().toJSON(), about:'message data parsed', rpcType, rpcHow, rpcWhat, randomEvent, rpcResult}, '\n,')
|
// globalThis.wo?.cclog?.({ about:'message data parsed', rpcType, rpcHow, rpcWhat, randomEvent, rpcResult})
|
||||||
switch (rpcType) {
|
switch (rpcType) {
|
||||||
case 'SEND_REQUEST':
|
case 'SEND_REQUEST':
|
||||||
// 接收到异步的远程调用
|
// 接收到异步的远程调用
|
||||||
// console.log({ _at: new Date().toJSON(), about:'被调方 收到 SEND_REQUEST', rpcHow, rpcWhat}, '\n,')
|
// globalThis.wo?.cclog?.({ 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({ _at: new Date().toJSON(),about:'主调方 收到 SEND_RESULT', rpcResult}, '\n,')
|
// globalThis.wo?.cclog?.({ 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({ _at: new Date().toJSON(),about:'被调方 收到 SEND_NOFITY', rpcHow, rpcWhat})
|
// globalThis.wo?.cclog?.({ 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({ _at: new Date().toJSON(), about: '[onmessage] unknown rpc', rpcHow, rpcWhat }, '\n,')
|
else globalThis.wo?.ccerror?.({ about: '[onmessage] unknown rpc', rpcHow, rpcWhat })
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
console.error({ _at: new Date().toJSON(), about: '[onmessage] invalid rpc data', data, exception }, '\n,')
|
globalThis.wo?.ccerror?.({ 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({ _at: new Date().toJSON(),about:'randomEvent is randomized', randomEvent})
|
// globalThis.wo?.cclog?.({ 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({ _at: new Date().toJSON(),about:'in callback', randomEvent})
|
// globalThis.wo?.cclog?.({ 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) {
|
||||||
|
|||||||
@@ -22,6 +22,12 @@
|
|||||||
*.nosf/
|
*.nosf/
|
||||||
*.nosf.*/
|
*.nosf.*/
|
||||||
|
|
||||||
|
## everything 'git pull or fetch' will update `.git/FETCH_HEAD`, even if the content doesn't change. To avoid too many useless updates of this file in Seafile history:
|
||||||
|
FETCH_HEAD
|
||||||
|
*/FETCH_HEAD
|
||||||
|
|
||||||
|
.Trash/
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*/.DS_Store
|
*/.DS_Store
|
||||||
|
|
||||||
@@ -48,12 +54,18 @@ _desktop.ini
|
|||||||
node_modules/
|
node_modules/
|
||||||
*/node_modules/
|
*/node_modules/
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
*/package-lock.json
|
||||||
|
|
||||||
pages4loader.json5
|
pages4loader.json5
|
||||||
|
*/pages4loader.json5
|
||||||
|
|
||||||
.deploy_git/
|
.deploy_git/
|
||||||
*/.deploy_git/
|
*/.deploy_git/
|
||||||
|
|
||||||
|
# next.js 项目
|
||||||
|
.next/
|
||||||
|
*/.next/
|
||||||
|
|
||||||
# HBuilder 目录
|
# HBuilder 目录
|
||||||
unpackage/
|
unpackage/
|
||||||
*/unpackage/
|
*/unpackage/
|
||||||
|
|||||||
Reference in New Issue
Block a user