重构 websocket

This commit is contained in:
ExplodingDragon
2025-11-20 23:56:14 +08:00
parent 35df220bea
commit 827f9bd273
10 changed files with 161 additions and 84 deletions

View File

@@ -1,19 +1,17 @@
(async ()=>{
let ws = websocket();
let shouldExit = false;
while (!shouldExit) {
while (true) {
let data = await ws.readText();
switch (data) {
case "exit":
shouldExit = true;
break;
return
case "panic":
throw Error("错误");
case "date":
ws.writeText(new Date().toJSON())
await ws.writeText(new Date().toJSON())
break
default:
ws.writeText("收到信息:" + data)
await ws.writeText("收到信息:" + data)
break;
}
}

View File

@@ -8,8 +8,8 @@ const ws = websocket();
async function eventPull() {
while (true) {
const data = await event.pull('messages')
ws.writeText(data);
const data = await event.load('messages')
await ws.writeText(data);
}
}
@@ -27,5 +27,5 @@ async function messagePull() {
}
(async () => {
await Promise.all(eventPull(), messagePull())
await Promise.all([eventPull(), messagePull()])
})()