Merge pull request #31 from Legonzaur/main

Add websockets
Closes #28
This commit is contained in:
Legonzaur
2023-05-02 12:26:41 +02:00
committed by GitHub
4 changed files with 100 additions and 5 deletions
+2 -2
View File
@@ -25,9 +25,9 @@
{ {
"type": "node", "type": "node",
"request": "launch", "request": "launch",
"name": "Launch Process API", "name": "Launch Websocket",
"skipFiles": ["<node_internals>/**"], "skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/src/processMain.ts", "program": "${workspaceFolder}/src/websocket.ts",
"preLaunchTask": "tsc: build - tsconfig.json", "preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/out/**/*.js"] "outFiles": ["${workspaceFolder}/out/**/*.js"]
} }
+31 -1
View File
@@ -5,6 +5,7 @@
"packages": { "packages": {
"": { "": {
"dependencies": { "dependencies": {
"@types/ws": "^8.5.4",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
@@ -13,7 +14,8 @@
"kysely": "^0.23.4", "kysely": "^0.23.4",
"pg": "^8.9.0", "pg": "^8.9.0",
"redis": "^4.6.5", "redis": "^4.6.5",
"ts-node": "^10.9.1" "ts-node": "^10.9.1",
"ws": "^8.13.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.21.0", "@babel/core": "^7.21.0",
@@ -2888,6 +2890,14 @@
"integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==",
"dev": true "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": { "node_modules/@types/yargs": {
"version": "17.0.22", "version": "17.0.22",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.22.tgz", "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": "^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": { "node_modules/xtend": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
+4 -1
View File
@@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"@types/ws": "^8.5.4",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
@@ -8,7 +9,8 @@
"kysely": "^0.23.4", "kysely": "^0.23.4",
"pg": "^8.9.0", "pg": "^8.9.0",
"redis": "^4.6.5", "redis": "^4.6.5",
"ts-node": "^10.9.1" "ts-node": "^10.9.1",
"ws": "^8.13.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.21.0", "@babel/core": "^7.21.0",
@@ -33,6 +35,7 @@
"build": "npx tsc", "build": "npx tsc",
"startServer": "node out/serverMain.js", "startServer": "node out/serverMain.js",
"startClient": "node out/clientMain.js", "startClient": "node out/clientMain.js",
"startWebsocket": "node out/websocket.js",
"documentation": "redocly build-docs .\\docs\\v2.yml --output=docs\\index.html", "documentation": "redocly build-docs .\\docs\\v2.yml --output=docs\\index.html",
"test": "jest --runInBand" "test": "jest --runInBand"
} }
+62
View File
@@ -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
})
)
}
})
})
})