-
Notifications
You must be signed in to change notification settings - Fork 111
/
ws.ts
47 lines (45 loc) · 1.19 KB
/
ws.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { defineWebSocket, eventHandler } from "vinxi/http"
import { exportLib } from "src/export"
import type { ExportFnProps, WSMessgae } from "@/types"
import { delay } from "@/utils"
let stop = false
export default eventHandler({
handler: () => { },
websocket: defineWebSocket({
async open() {
console.log("webSocket opened")
},
async message(peer, message) {
const msg = JSON.parse(message.text()) as WSMessgae<ExportFnProps>
let last = 0
switch (msg.type) {
case "export":
stop = false
exportLib({
...msg.data!,
async fnEvery(log) {
// 留 100 ms 接收 stop,并且确保不要发送太快,避免 UI 刷新不及时
const now = Date.now()
await delay(now - last > 100 ? 1 : 100)
peer.send({
...log,
stop,
})
last = now
return stop
},
})
break
case "stop":
stop = true
break
}
},
async close() {
console.log("webSocket closed")
},
async error() {
console.log("webSocket error")
},
}),
})