1 Star 0 Fork 0

HDTianRu/node-omg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
coromega.js 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
HDTianRu 提交于 2024-10-02 12:16 . frist commit
import api from "./api.js"
import { ulid } from "ulid"
import WebSocket from "ws"
class omega {
ws = Object.create(null)
echo = {}
cbs = {}
timeout = 30*1000
async init() {
if (!!this.ws?.url) return
return new Promise((resolve) => {
this.connect("ws://127.0.0.1:3291", resolve)
})
}
connect(url, resolve) {
this.ws = new WebSocket(url)
this.ws
.on("open", () => {
let type = "event"
for (const [ event, _ ] of Object.entries(this.cbs)) {
this.ws.send(JSON.stringify({ type, event }))
}
resolve()
logger.info("ws已连接")
})
.on("error", (err) => logger.error(err.code))
.on("close", async () => {
logger.warn("ws已断开")
await this.sleep(10*1000)
this.connect(url, resolve)
})
.on("message", (data) => this.message(data, this.ws))
}
sleep(time, promise) {
if (promise) return Promise.race([promise, this.sleep(time)])
return new Promise(resolve => setTimeout(resolve, time))
}
sendApi(data, ws) {
const { type, event, action, param, callback } = data
if (type == "event") {
if (this.cbs[event] === undefined) {
this.cbs[event] = [callback]
ws.send(JSON.stringify({ type, event }))
} else {
this.cbs[event].push(callback)
}
return
}
const echo = ulid()
const request = { type, action, param, echo }
ws.send(JSON.stringify(request))
const error = Error()
return new Promise((resolve, reject) =>
this.echo[echo] = {
request, resolve, reject, error,
timeout: setTimeout(() => {
reject(Object.assign(error, request, { timeout: this.timeout }))
delete this.echo[echo]
ws.terminate()
}, this.timeout),
}
)
}
message(data, ws) {
try {
data = JSON.parse(data)
} catch (err) {}
if (data.event && this.cbs[data.event]) {
for (const callback of this.cbs[data.event]) {
callback(data.data)
}
}
else if (data.echo && this.echo[data.echo]) {
this.echo[data.echo].resolve(data.data ? new Proxy(data, {
get: (target, prop) => target.data[prop] ?? target[prop],
}) : data)
clearTimeout(this.echo[data.echo].timeout)
delete this.echo[data.echo]
} else {
logger.warn("未知消息:", data)
}
}
}
export default new Proxy(new omega(), {
get: (target, prop) => {
return target[prop] || ((...args) => {
return target.sendApi(api[prop](...args), target.ws)
})
}
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/HDTianRu/node-omg.git
[email protected]:HDTianRu/node-omg.git
HDTianRu
node-omg
node-omg
main

搜索帮助