allows connections
This commit is contained in:
48
package-lock.json
generated
48
package-lock.json
generated
@@ -8,8 +8,12 @@
|
||||
"name": "webdj.backend",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ws": "^8.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.12",
|
||||
"@types/ws": "^8.2.2",
|
||||
"npm-watch": "^0.11.0",
|
||||
"typescript": "^4.5.3"
|
||||
}
|
||||
@@ -41,6 +45,15 @@
|
||||
"integrity": "sha512-+2Iggwg7PxoO5Kyhvsq9VarmPbIelXP070HMImEpbtGCoyWNINQj4wzjbQCXzdHTRXnqufutJb5KAURZANNBAw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/ws": {
|
||||
"version": "8.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.2.2.tgz",
|
||||
"integrity": "sha512-NOn5eIcgWLOo6qW8AcuLZ7G8PycXu0xTxxkS6Q18VWFxgPUSOwV0pBj2a/4viNZVu25i7RIB7GttdkAIUUXOOg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/abbrev": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
|
||||
@@ -1361,6 +1374,26 @@
|
||||
"typedarray-to-buffer": "^3.1.5"
|
||||
}
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.3.0.tgz",
|
||||
"integrity": "sha512-Gs5EZtpqZzLvmIM59w4igITU57lrtYVFneaa434VROv4thzJyV6UjIL3D42lslWlI+D4KzLYnxSwtfuiO79sNw==",
|
||||
"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/xdg-basedir": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",
|
||||
@@ -1399,6 +1432,15 @@
|
||||
"integrity": "sha512-+2Iggwg7PxoO5Kyhvsq9VarmPbIelXP070HMImEpbtGCoyWNINQj4wzjbQCXzdHTRXnqufutJb5KAURZANNBAw==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/ws": {
|
||||
"version": "8.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.2.2.tgz",
|
||||
"integrity": "sha512-NOn5eIcgWLOo6qW8AcuLZ7G8PycXu0xTxxkS6Q18VWFxgPUSOwV0pBj2a/4viNZVu25i7RIB7GttdkAIUUXOOg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"abbrev": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
|
||||
@@ -2401,6 +2443,12 @@
|
||||
"typedarray-to-buffer": "^3.1.5"
|
||||
}
|
||||
},
|
||||
"ws": {
|
||||
"version": "8.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.3.0.tgz",
|
||||
"integrity": "sha512-Gs5EZtpqZzLvmIM59w4igITU57lrtYVFneaa434VROv4thzJyV6UjIL3D42lslWlI+D4KzLYnxSwtfuiO79sNw==",
|
||||
"requires": {}
|
||||
},
|
||||
"xdg-basedir": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node .",
|
||||
"watch": "npm-watch"
|
||||
"watch": "npm-watch",
|
||||
"dev": "npm-watch"
|
||||
},
|
||||
"watch": {
|
||||
"build": {
|
||||
@@ -29,7 +30,11 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.12",
|
||||
"@types/ws": "^8.2.2",
|
||||
"npm-watch": "^0.11.0",
|
||||
"typescript": "^4.5.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": "^8.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
33
src/connection.ts
Normal file
33
src/connection.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import WebSocket from 'ws';
|
||||
|
||||
//an individual connection
|
||||
export class Connection {
|
||||
|
||||
private _ws: WebSocket;
|
||||
private _data: initConnectData;
|
||||
isAlive: boolean;
|
||||
|
||||
constructor(ws: WebSocket, data: initConnectData) {
|
||||
this._ws = ws;
|
||||
this._data = data;
|
||||
this.isAlive = true;
|
||||
|
||||
ws.on('pong', () => this.isAlive = true);
|
||||
|
||||
}
|
||||
|
||||
public ping() {
|
||||
this.isAlive = false;
|
||||
this._ws.ping();
|
||||
}
|
||||
|
||||
public terminate() {
|
||||
this._ws.terminate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//data sent to the server to initialize the connecction
|
||||
export interface initConnectData {
|
||||
id: string //change to allow login through discord
|
||||
}
|
||||
59
src/index.ts
Normal file
59
src/index.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import WebSocket, { WebSocketServer } from 'ws';
|
||||
import { Connection, initConnectData } from './connection';
|
||||
|
||||
const wss = new WebSocketServer({ port: 8001 }); //ill let nginx deal with https
|
||||
|
||||
//all current connections
|
||||
const Connections: { [keys: string]: Connection } = {};
|
||||
|
||||
//new client joins
|
||||
wss.on('connection', ws => {
|
||||
|
||||
ws.once('message', data => {
|
||||
|
||||
let dataJson: initConnectData;
|
||||
|
||||
try {
|
||||
dataJson = JSON.parse(data.toString()) as initConnectData;
|
||||
initConnection(ws, dataJson);
|
||||
} catch (e) {
|
||||
ws.terminate();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
//verify data and add to connections list
|
||||
function initConnection(ws: WebSocket, data: initConnectData) {
|
||||
|
||||
if (data.id in Connections) {
|
||||
ws.terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
Connections[data.id] = new Connection(ws, data);
|
||||
}
|
||||
|
||||
//ensure all connections are alive
|
||||
const interval = setInterval(() => {
|
||||
for (let key in Connections) {
|
||||
let conn = Connections[key];
|
||||
|
||||
if (conn.isAlive)
|
||||
conn.ping();
|
||||
else {
|
||||
conn.terminate();
|
||||
delete Connections[key];
|
||||
}
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
1. connect to ws server
|
||||
2. send `initConnectData` as a string (will change to login with discord)
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user