add keepalive ping to websocket
This commit is contained in:
+20
-2
@@ -1,7 +1,7 @@
|
|||||||
import * as dotenv from 'dotenv'
|
import * as dotenv from 'dotenv'
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
import { Client } from 'pg'
|
import { Client } from 'pg'
|
||||||
import { WebSocketServer } from 'ws'
|
import { WebSocket, WebSocketServer } from 'ws'
|
||||||
import { KillTable } from './db/model'
|
import { KillTable } from './db/model'
|
||||||
|
|
||||||
const port = 3002
|
const port = 3002
|
||||||
@@ -14,7 +14,7 @@ export const pgClient = new Client({
|
|||||||
password: process.env.POSTGRES_PASSWORD
|
password: process.env.POSTGRES_PASSWORD
|
||||||
})
|
})
|
||||||
|
|
||||||
wss.on('connection', function connection(ws: WebSocket, req) {
|
wss.on('connection', function connection(ws: WebSocket & { isAlive: boolean }, req) {
|
||||||
const ip =
|
const ip =
|
||||||
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
|
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
|
||||||
req.socket.remoteAddress
|
req.socket.remoteAddress
|
||||||
@@ -29,8 +29,26 @@ wss.on('connection', function connection(ws: WebSocket, req) {
|
|||||||
new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size
|
new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
ws.onmessage = function (msg) {
|
||||||
|
if (msg.data === "pong") {
|
||||||
|
ws.isAlive = true
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const interval = setInterval(function ping() {
|
||||||
|
wss.clients.forEach(function each(ws) {
|
||||||
|
if ((ws as WebSocket & { isAlive: boolean }).isAlive === false) return ws.terminate();
|
||||||
|
|
||||||
|
(ws as WebSocket & { isAlive: boolean }).isAlive = false;
|
||||||
|
ws.ping();
|
||||||
|
});
|
||||||
|
}, 30000);
|
||||||
|
|
||||||
|
wss.on('close', function close() {
|
||||||
|
clearInterval(interval);
|
||||||
|
});
|
||||||
|
|
||||||
export default new Promise(async (resolve, reject) => {
|
export default new Promise(async (resolve, reject) => {
|
||||||
await pgClient.connect()
|
await pgClient.connect()
|
||||||
await pgClient.query('LISTEN new_kill')
|
await pgClient.query('LISTEN new_kill')
|
||||||
|
|||||||
Reference in New Issue
Block a user