2020-10-21 07:05:58 +08:00
|
|
|
import { Router } from "express"
|
2021-01-29 02:47:50 +08:00
|
|
|
import { wss, Router as WsRouter } from "../wsRouter"
|
2020-08-31 23:29:12 +08:00
|
|
|
|
2020-10-21 07:05:58 +08:00
|
|
|
export const router = Router()
|
2020-08-31 23:29:12 +08:00
|
|
|
|
2020-10-21 07:05:58 +08:00
|
|
|
router.get("/", (req, res) => {
|
|
|
|
res.json({
|
|
|
|
status: req.heart.alive() ? "alive" : "expired",
|
|
|
|
lastHeartbeat: req.heart.lastHeartbeat,
|
|
|
|
})
|
|
|
|
})
|
2021-01-29 02:47:50 +08:00
|
|
|
|
|
|
|
export const wsRouter = WsRouter()
|
|
|
|
|
|
|
|
wsRouter.ws("/", async (req) => {
|
2021-02-17 04:14:52 +08:00
|
|
|
wss.handleUpgrade(req, req.ws, req.head, (ws) => {
|
|
|
|
ws.addEventListener("message", () => {
|
2021-01-29 02:47:50 +08:00
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
event: "health",
|
|
|
|
status: req.heart.alive() ? "alive" : "expired",
|
|
|
|
lastHeartbeat: req.heart.lastHeartbeat,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
2021-02-17 04:14:52 +08:00
|
|
|
req.ws.resume()
|
2021-01-29 02:47:50 +08:00
|
|
|
})
|
|
|
|
})
|