added express js
This commit is contained in:
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user