重构 websocket
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()])
|
||||
})()
|
||||
Reference in New Issue
Block a user