From a949b442331e5e12e52679ae6ea0a4acc42f9e4e Mon Sep 17 00:00:00 2001 From: Jonas Kappa Date: Sat, 30 Aug 2025 00:56:30 +0200 Subject: [PATCH] init --- .gitignore | 140 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 111 ++++++++++++++++++++++++++++++++++++ package-lock.json | 37 ++++++++++++ package.json | 15 +++++ 4 files changed, 303 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9aa34f --- /dev/null +++ b/.gitignore @@ -0,0 +1,140 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.* +!.env.example + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist +.output + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Sveltekit cache directory +.svelte-kit/ + +# vitepress build output +**/.vitepress/dist + +# vitepress cache directory +**/.vitepress/cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# Firebase cache directory +.firebase/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v3 +.pnp.* +.yarn/* +!.yarn/patches +!.yarn/plugins +!.yarn/releases +!.yarn/sdks +!.yarn/versions + +# Vite logs files +vite.config.js.timestamp-* +vite.config.ts.timestamp-* diff --git a/index.js b/index.js new file mode 100644 index 0000000..1a212ae --- /dev/null +++ b/index.js @@ -0,0 +1,111 @@ +import WebSocket, { WebSocketServer } from 'ws'; + +let hostConnection; +let displayConnection; + +const wss = new WebSocketServer({ + port: 12345, +}, () => { + console.log("Websocket Server started\nListening on Port 12345") +}); + +wss.on('connection', (ws) => { + 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(); + } + }) + + // ws.send('Connected to server'); +}); + +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"); + } +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..d7952bd --- /dev/null +++ b/package-lock.json @@ -0,0 +1,37 @@ +{ + "name": "jeopardyserver", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "jeopardyserver", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "ws": "^8.18.3" + } + }, + "node_modules/ws": { + "version": "8.18.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz", + "integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==", + "license": "MIT", + "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 + } + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..adda98d --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "jeopardyserver", + "version": "1.0.0", + "description": "", + "license": "ISC", + "author": "", + "type": "module", + "main": "index.js", + "scripts": { + "dev": "node index.js" + }, + "dependencies": { + "ws": "^8.18.3" + } +}