init
This commit is contained in:
111
index.js
Normal file
111
index.js
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user