allows connections

This commit is contained in:
2021-12-14 15:05:46 -06:00
parent 258b703247
commit 6d6444e541
4 changed files with 2550 additions and 2405 deletions

48
package-lock.json generated
View File

@@ -8,8 +8,12 @@
"name": "webdj.backend", "name": "webdj.backend",
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": {
"ws": "^8.3.0"
},
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.12", "@types/node": "^16.11.12",
"@types/ws": "^8.2.2",
"npm-watch": "^0.11.0", "npm-watch": "^0.11.0",
"typescript": "^4.5.3" "typescript": "^4.5.3"
} }
@@ -41,6 +45,15 @@
"integrity": "sha512-+2Iggwg7PxoO5Kyhvsq9VarmPbIelXP070HMImEpbtGCoyWNINQj4wzjbQCXzdHTRXnqufutJb5KAURZANNBAw==", "integrity": "sha512-+2Iggwg7PxoO5Kyhvsq9VarmPbIelXP070HMImEpbtGCoyWNINQj4wzjbQCXzdHTRXnqufutJb5KAURZANNBAw==",
"dev": true "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": { "node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@@ -1361,6 +1374,26 @@
"typedarray-to-buffer": "^3.1.5" "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": { "node_modules/xdg-basedir": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz", "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",
@@ -1399,6 +1432,15 @@
"integrity": "sha512-+2Iggwg7PxoO5Kyhvsq9VarmPbIelXP070HMImEpbtGCoyWNINQj4wzjbQCXzdHTRXnqufutJb5KAURZANNBAw==", "integrity": "sha512-+2Iggwg7PxoO5Kyhvsq9VarmPbIelXP070HMImEpbtGCoyWNINQj4wzjbQCXzdHTRXnqufutJb5KAURZANNBAw==",
"dev": true "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": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@@ -2401,6 +2443,12 @@
"typedarray-to-buffer": "^3.1.5" "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": { "xdg-basedir": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz", "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",

View File

@@ -6,7 +6,8 @@
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"start": "node .", "start": "node .",
"watch": "npm-watch" "watch": "npm-watch",
"dev": "npm-watch"
}, },
"watch": { "watch": {
"build": { "build": {
@@ -29,7 +30,11 @@
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.12", "@types/node": "^16.11.12",
"@types/ws": "^8.2.2",
"npm-watch": "^0.11.0", "npm-watch": "^0.11.0",
"typescript": "^4.5.3" "typescript": "^4.5.3"
},
"dependencies": {
"ws": "^8.3.0"
} }
} }

33
src/connection.ts Normal file
View 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
View 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)
*/