Add websockets
This commit is contained in:
Vendored
+2
-2
@@ -25,9 +25,9 @@
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Process API",
|
||||
"name": "Launch Websocket",
|
||||
"skipFiles": ["<node_internals>/**"],
|
||||
"program": "${workspaceFolder}/src/processMain.ts",
|
||||
"program": "${workspaceFolder}/src/websocket.ts",
|
||||
"preLaunchTask": "tsc: build - tsconfig.json",
|
||||
"outFiles": ["${workspaceFolder}/out/**/*.js"]
|
||||
}
|
||||
|
||||
Generated
+31
-1
@@ -5,6 +5,7 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"@types/ws": "^8.5.4",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
@@ -13,7 +14,8 @@
|
||||
"kysely": "^0.23.4",
|
||||
"pg": "^8.9.0",
|
||||
"redis": "^4.6.5",
|
||||
"ts-node": "^10.9.1"
|
||||
"ts-node": "^10.9.1",
|
||||
"ws": "^8.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.21.0",
|
||||
@@ -2888,6 +2890,14 @@
|
||||
"integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/ws": {
|
||||
"version": "8.5.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.4.tgz",
|
||||
"integrity": "sha512-zdQDHKUgcX/zBc4GrwsE/7dVdAD8JR4EuiAXiiUhhfyIJXXb2+PrGshFyeXWQPMmmZ2XxgaqclgpIC7eTXc1mg==",
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/yargs": {
|
||||
"version": "17.0.22",
|
||||
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.22.tgz",
|
||||
@@ -8286,6 +8296,26 @@
|
||||
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.13.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz",
|
||||
"integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": ">=5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/xtend": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||
|
||||
+5
-2
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"@types/ws": "^8.5.4",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
@@ -8,7 +9,8 @@
|
||||
"kysely": "^0.23.4",
|
||||
"pg": "^8.9.0",
|
||||
"redis": "^4.6.5",
|
||||
"ts-node": "^10.9.1"
|
||||
"ts-node": "^10.9.1",
|
||||
"ws": "^8.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.21.0",
|
||||
@@ -33,7 +35,8 @@
|
||||
"build": "npx tsc",
|
||||
"startServer": "node out/serverMain.js",
|
||||
"startClient": "node out/clientMain.js",
|
||||
"startWebsocket": "node out/websocket.js",
|
||||
"documentation": "redocly build-docs .\\docs\\v2.yml --output=docs\\index.html",
|
||||
"test": "jest --runInBand"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import * as dotenv from 'dotenv'
|
||||
dotenv.config()
|
||||
import { Client } from 'pg'
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { KillTable } from './db/model'
|
||||
|
||||
const port = 3002
|
||||
const wss = new WebSocketServer({ port })
|
||||
|
||||
export const pgClient = new Client({
|
||||
host: process.env.POSTGRES_HOST,
|
||||
database: process.env.POSTGRES_DATABASE,
|
||||
user: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD
|
||||
})
|
||||
|
||||
wss.on('connection', function connection(ws: WebSocket, req) {
|
||||
const ip =
|
||||
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
|
||||
req.socket.remoteAddress
|
||||
console.log(
|
||||
new Date().toLocaleString() + ',' + ip + ',connect,' + wss.clients.size
|
||||
)
|
||||
ws.onclose = function () {
|
||||
const ip =
|
||||
req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ||
|
||||
req.socket.remoteAddress
|
||||
console.log(
|
||||
new Date().toLocaleString() + ',' + ip + ',close,' + wss.clients.size
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
export default new Promise(async (resolve, reject) => {
|
||||
await pgClient.connect()
|
||||
await pgClient.query('LISTEN new_kill')
|
||||
|
||||
pgClient.on('notification', (data) => {
|
||||
if (!data.payload) return
|
||||
const payload = JSON.parse(data.payload) as KillTable
|
||||
wss.clients.forEach(function (client) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(
|
||||
JSON.stringify({
|
||||
attacker_id: payload.attacker_id,
|
||||
attacker_name: payload.attacker_name,
|
||||
cause_of_death: payload.cause_of_death,
|
||||
victim_id: payload.victim_id,
|
||||
victim_name: payload.victim_name,
|
||||
attacker_current_weapon: payload.attacker_current_weapon,
|
||||
victim_current_weapon: payload.victim_current_weapon,
|
||||
distance: payload.distance,
|
||||
game_mode: payload.game_mode,
|
||||
servername: payload.servername,
|
||||
map: payload.map,
|
||||
host: payload.host
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user