Compare commits
2 Commits
dac037809c
...
1a151b197b
Author | SHA1 | Date | |
---|---|---|---|
1a151b197b | |||
70180c5d5f |
28
src/main.ts
28
src/main.ts
@ -8,6 +8,7 @@ import {
|
||||
import striptags from "striptags";
|
||||
import { PrismaClient } from "../generated/prisma/client.js";
|
||||
import { createWebsocket } from "./websocket.js";
|
||||
import { WebSocket } from "ws";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
@ -151,7 +152,24 @@ const postReplyToStatus = async (
|
||||
}
|
||||
};
|
||||
|
||||
const ws = createWebsocket();
|
||||
let ws = createWebsocket();
|
||||
|
||||
const reconnect = (ws: WebSocket) => {
|
||||
if (ws) {
|
||||
ws.close();
|
||||
}
|
||||
return createWebsocket();
|
||||
};
|
||||
|
||||
ws.on("close", () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
if (ws.readyState !== WebSocket.OPEN) {
|
||||
setTimeout(() => {
|
||||
ws = reconnect(ws);
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("upgrade", () => {
|
||||
console.log(
|
||||
@ -159,10 +177,6 @@ ws.on("upgrade", () => {
|
||||
);
|
||||
});
|
||||
|
||||
ws.on("close", (event: CloseEvent) => {
|
||||
console.log(`Connection closed: ${event.reason}`);
|
||||
});
|
||||
|
||||
ws.on("open", () => {
|
||||
setInterval(() => {
|
||||
ws.send(JSON.stringify({ type: "ping" }));
|
||||
@ -170,6 +184,10 @@ ws.on("open", () => {
|
||||
}, 20000);
|
||||
});
|
||||
|
||||
ws.on("pong", (data) => {
|
||||
console.log(`Pong received: ${JSON.stringify(data.toString("utf-8"))}`);
|
||||
});
|
||||
|
||||
ws.on("message", async (data) => {
|
||||
try {
|
||||
const message: WSEvent = JSON.parse(data.toString("utf-8"));
|
||||
|
Reference in New Issue
Block a user