added express js
This commit is contained in:
9
auth.js
Normal file
9
auth.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import {db} from './db.js';
|
||||
|
||||
const users = db.collection('users');
|
||||
|
||||
export function initAuth(app) {
|
||||
app.get('/auth/login', (req, res) => {
|
||||
|
||||
})
|
||||
}
|
||||
20
db.js
Normal file
20
db.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { MongoClient } from "mongodb";
|
||||
|
||||
const client = new MongoClient(`mongodb://${process.env.MONGODB_USER}:${process.env.MONGODB_PASSWORD}@${process.env.MONGODB_URL}`);
|
||||
|
||||
const dbName = `jeopardy`;
|
||||
|
||||
/**
|
||||
* @type {Db}
|
||||
*/
|
||||
export let db;
|
||||
|
||||
export async function initDbConnection() {
|
||||
await client.connect();
|
||||
console.log('Connected successfully to mongodb');
|
||||
db = client.db(dbName);
|
||||
}
|
||||
|
||||
export function close() {
|
||||
client.close();
|
||||
}
|
||||
122
index.js
122
index.js
@@ -1,111 +1,23 @@
|
||||
import WebSocket, { WebSocketServer } from 'ws';
|
||||
import express from "express";
|
||||
import expressWs from "express-ws";
|
||||
import { initWebsocket } from "./websocket.js";
|
||||
import { initAuth } from "./auth.js";
|
||||
import { close as closeDbConnection, initDbConnection } from "./db.js";
|
||||
const app = express();
|
||||
const appWs = expressWs(app);
|
||||
const port = 12345;
|
||||
|
||||
let hostConnection;
|
||||
let displayConnection;
|
||||
|
||||
const wss = new WebSocketServer({
|
||||
port: 12345,
|
||||
}, () => {
|
||||
console.log("Websocket Server started\nListening on Port 12345")
|
||||
process.on('exit', function() {
|
||||
console.log('Shutting down...');
|
||||
console.log('Closing db connection...');
|
||||
closeDbConnection();
|
||||
});
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
console.log("Trying to connect");
|
||||
ws.on('error', console.error);
|
||||
await initDbConnection();
|
||||
|
||||
ws.on('message', (data) => {
|
||||
if (ws == hostConnection || ws == displayConnection) return;
|
||||
console.log('received: %s', data);
|
||||
if (data == "HOST") {
|
||||
if (hostConnection === undefined) {
|
||||
hostConnection = ws;
|
||||
initHostConnection();
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.send("ERROR HOST");
|
||||
ws.close();
|
||||
}
|
||||
} else if (data == "DISPLAY") {
|
||||
if (displayConnection === undefined) {
|
||||
displayConnection = ws;
|
||||
initDisplayConnection();
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.send("ERROR DISPLAY");
|
||||
ws.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.send("ERROR MESSAGE");
|
||||
ws.close();
|
||||
}
|
||||
})
|
||||
initAuth(app);
|
||||
initWebsocket(app);
|
||||
|
||||
// ws.send('Connected to server');
|
||||
app.listen(port, () => {
|
||||
console.log(`Listening on port ${port}`);
|
||||
});
|
||||
|
||||
function initHostConnection() {
|
||||
console.log("Initialize Host connection...");
|
||||
|
||||
hostConnection.on('message', (data) => {
|
||||
console.log("[HOST] " + data);
|
||||
if (displayConnection) {
|
||||
displayConnection.send("" + data);
|
||||
}
|
||||
});
|
||||
|
||||
hostConnection.on('error', (data) => {
|
||||
console.error("[HOST] " + data);
|
||||
hostConnection = undefined;
|
||||
if (displayConnection) {
|
||||
displayConnection.send("HOST-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
hostConnection.on('close', (code, reason) => {
|
||||
console.error("[HOST] " + code + " " + reason);
|
||||
hostConnection = undefined;
|
||||
if (displayConnection) {
|
||||
displayConnection.send("HOST-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
hostConnection.send("HOST");
|
||||
if (displayConnection) {
|
||||
displayConnection.send("HOST-CONNECTED");
|
||||
hostConnection.send("DISPLAY-CONNECTED");
|
||||
}
|
||||
}
|
||||
|
||||
function initDisplayConnection() {
|
||||
console.log("Initialize Display connection...");
|
||||
|
||||
displayConnection.on('message', (data) => {
|
||||
console.log("[DISPLAY] " + data);
|
||||
});
|
||||
|
||||
displayConnection.on('error', (data) => {
|
||||
console.error("[DISPLAY] " + data);
|
||||
displayConnection = undefined;
|
||||
if (hostConnection) {
|
||||
hostConnection.send("DISPLAY-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
displayConnection.on('close', (code, reason) => {
|
||||
console.error("[DISPLAY] " + code + " " + reason);
|
||||
displayConnection = undefined;
|
||||
if (hostConnection) {
|
||||
hostConnection.send("DISPLAY-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
displayConnection.send("DISPLAY");
|
||||
if (hostConnection) {
|
||||
hostConnection.send("DISPLAY-CONNECTED");
|
||||
displayConnection.send("HOST-CONNECTED");
|
||||
}
|
||||
}
|
||||
|
||||
1118
package-lock.json
generated
1118
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,13 @@
|
||||
"docker-build": "docker build -t jeopardyserver ."
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/express": "^5.0.3",
|
||||
"express": "^5.1.0",
|
||||
"express-ws": "^5.0.2",
|
||||
"mongodb": "^6.20.0",
|
||||
"ws": "^8.18.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^24.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
2
requests/test.http
Normal file
2
requests/test.http
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
GET http://localhost:12345/ HTTP/1.1
|
||||
103
websocket.js
Normal file
103
websocket.js
Normal file
@@ -0,0 +1,103 @@
|
||||
let hostConnection;
|
||||
let displayConnection;
|
||||
|
||||
function initHostConnection() {
|
||||
console.log("Initialize Host connection...");
|
||||
|
||||
hostConnection.on('message', (data) => {
|
||||
console.log("[HOST] " + data);
|
||||
if (displayConnection) {
|
||||
displayConnection.send("" + data);
|
||||
}
|
||||
});
|
||||
|
||||
hostConnection.on('error', (data) => {
|
||||
console.error("[HOST] " + data);
|
||||
hostConnection = undefined;
|
||||
if (displayConnection) {
|
||||
displayConnection.send("HOST-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
hostConnection.on('close', (code, reason) => {
|
||||
console.error("[HOST] " + code + " " + reason);
|
||||
hostConnection = undefined;
|
||||
if (displayConnection) {
|
||||
displayConnection.send("HOST-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
hostConnection.send("HOST");
|
||||
if (displayConnection) {
|
||||
displayConnection.send("HOST-CONNECTED");
|
||||
hostConnection.send("DISPLAY-CONNECTED");
|
||||
}
|
||||
}
|
||||
|
||||
function initDisplayConnection() {
|
||||
console.log("Initialize Display connection...");
|
||||
|
||||
displayConnection.on('message', (data) => {
|
||||
console.log("[DISPLAY] " + data);
|
||||
});
|
||||
|
||||
displayConnection.on('error', (data) => {
|
||||
console.error("[DISPLAY] " + data);
|
||||
displayConnection = undefined;
|
||||
if (hostConnection) {
|
||||
hostConnection.send("DISPLAY-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
displayConnection.on('close', (code, reason) => {
|
||||
console.error("[DISPLAY] " + code + " " + reason);
|
||||
displayConnection = undefined;
|
||||
if (hostConnection) {
|
||||
hostConnection.send("DISPLAY-DISCONNECTED");
|
||||
}
|
||||
});
|
||||
|
||||
displayConnection.send("DISPLAY");
|
||||
if (hostConnection) {
|
||||
hostConnection.send("DISPLAY-CONNECTED");
|
||||
displayConnection.send("HOST-CONNECTED");
|
||||
}
|
||||
}
|
||||
|
||||
export function initWebsocket(app) {
|
||||
app.ws("/websocket", (ws, req) => {
|
||||
console.log("Trying to connect");
|
||||
ws.on('error', console.error);
|
||||
|
||||
ws.on('message', (data) => {
|
||||
if (ws == hostConnection || ws == displayConnection) return;
|
||||
console.log('received: %s', data);
|
||||
if (data == "HOST") {
|
||||
if (hostConnection === undefined) {
|
||||
hostConnection = ws;
|
||||
initHostConnection();
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.send("ERROR HOST");
|
||||
ws.close();
|
||||
}
|
||||
} else if (data == "DISPLAY") {
|
||||
if (displayConnection === undefined) {
|
||||
displayConnection = ws;
|
||||
initDisplayConnection();
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.send("ERROR DISPLAY");
|
||||
ws.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ws.send("ERROR MESSAGE");
|
||||
ws.close();
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user