Skip to content

Commit

Permalink
NEW: Working (ish) WebSocket connection to client
Browse files Browse the repository at this point in the history
  • Loading branch information
paulov-t committed Jul 29, 2022
1 parent 44d0709 commit 28d7e1b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 26 deletions.
57 changes: 49 additions & 8 deletions bin/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const app = require('../express/app');
const http = require('http');
const https = require('https');
const fs = require('fs');
// const ws = require('ws');
const { certificate } = require('./../core/server/certGenerator');
const { ConfigController } = require('./../src/Controllers/ConfigController')
const { ConfigController } = require('./../src/Controllers/ConfigController');
const { WebSocket } = require('ws');
const { logger } = require('../core/util/logger');
const utility = require('./../core/util/utility');
var serverIp = "127.0.0.1";

/**
Expand Down Expand Up @@ -75,15 +77,54 @@ if(serverConfig.runSimpleHttpServer === true) {
// socket.on('message', message => console.log(message));
// });


// const wss = new WebSocket.Server({ server: httpsServer, path: "/websocket" });

httpsServer.on('error', onError);
httpsServer.on('listening', () => { console.log("HTTPS Server listening on " + httpsServer.address().address + ":" + httpsServer.address().port) });
// httpsServer.on('upgrade', (request, socket, head) => {
// wsServer.handleUpgrade(request, socket, head, socket => {
// wsServer.emit('connection', socket, request);
// });
// });
httpsServer.on('listening', () => { logger.logSuccess("HTTPS Server listening on " + httpsServer.address().address + ":" + httpsServer.address().port) });
httpsServer.listen(port, serverIp, ()=>{
});
httpsServer.on("upgrade", (request, socket, head) => {
wss.handleUpgrade(request, socket, head, (websocket) => {
wss.emit("connection", websocket, request);
});
});
const wss = new WebSocket.Server({ host: serverIp, port: port + 1 });
wss.NextChannelId = utility.generateNewId();
wss.LastChannelId = utility.generateNewId();
// const wss = new WebSocket.Server({ server: httpsServer });
wss.on('listening', ws => {
logger.logInfo("WebSocket listening on " + wss.address().address + ":" + wss.address().port);
});
wss.on('connection', (ws, request, client) => {
// console.log(ws);
console.log(request);
// console.log(client);

if(wss.NextChannelId === wss.LastChannelId)
wss.NextChannelId = utility.generateNewId();

ws.ChannelId = wss.NextChannelId;
// Notify of Client connected
logger.logSuccess('WebSocket Client connected ');
// Send "ping" type back to the Client to make it happy
ws.send(JSON.stringify({ type: "ping", eventId: wss.ChannelId }));
// setInterval(()=>{
// ws.send(JSON.stringify({ type: "ping", eventId: ws.ChannelId }));
// }, 1000);
wss.LastChannelId = wss.NextChannelId;
// Respond to messages
ws.on('message', function message(data) {
logger.logInfo(`Received message ${data} from user ${client}`);
});
ws.on('upgrade', function message(data) {
logger.logInfo(`Received upgrade`);
});
});
wss.on('upgrade', function message(data) {
logger.logInfo(`Received upgrade`);
});
global.WebSocketServer = wss;

// /**
// * Normalize a port into a number, string, or false.
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@
"docdash": "^1.2.0",
"ejs": "^3.1.7",
"express": "^4.18.1",
"https": "^1.0.0",
"mathjs": "^10.6.1",
"selfsigned": "^2.0.1",
"uuid": "^8.3.2",
"write-json-file": "^4.3.0"
"write-json-file": "^4.3.0",
"ws": "^8.8.1"
},
"bin": "bin/www.js",
"pkg": {
"scripts": [ "core/**/*.js", "src/**/*.js" ],
"assets": [ "res/**/*", "db/**/*", "public/**/*", "express/views/**/*" ],
"scripts": [
"core/**/*.js",
"src/**/*.js"
],
"assets": [
"res/**/*",
"db/**/*",
"public/**/*",
"express/views/**/*"
],
"targets": [
"node16-win"
]
Expand Down
47 changes: 36 additions & 11 deletions src/Controllers/ResponseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,22 @@ class ResponseController

static getWebSocketUrl()
{
ConfigController.rebuildFromBaseConfigs();
var ws = ResponseController.getBackendUrl().replace("https", "ws");
// console.log("getWebSocketUrl:" + ws)
return ws;
const wss = global.WebSocketServer;
return `ws://${ResponseController.getWebSocketUrlWithoutWs()}`;
// ConfigController.rebuildFromBaseConfigs();
// var ws = ResponseController.getBackendUrl().replace("https", "wss");
// // console.log("getWebSocketUrl:" + ws)
// return ws;
}

static getWebSocketUrlWithoutWs()
{
const wss = global.WebSocketServer;
return `${wss.address().address}:${wss.address().port}`;
// ConfigController.rebuildFromBaseConfigs();
// var ws = ResponseController.getBackendUrl().replace("https", "wss");
// // console.log("getWebSocketUrl:" + ws)
// return ws;
}

static getMainUrl() {
Expand Down Expand Up @@ -119,12 +131,19 @@ class ResponseController
}

static getNotifier = (sessionID) => {
// return {
// server: ResponseController.getBackendUrl(), // this.httpServerHelper.buildUrl(),
// channel_id: sessionID,
// url: `${ResponseController.getBackendUrl()}/notifierServer/get/${sessionID}`,
// notifierServer: `${ResponseController.getBackendUrl()}/notifierServer/get/${sessionID}`,
// ws: `${ResponseController.getWebSocketUrl()}/notifierServer/getwebsocket/${sessionID}`
// }
global.WebSocketServer.NextChannelId = sessionID;
return {
server: ResponseController.getBackendUrl(), // this.httpServerHelper.buildUrl(),
channel_id: sessionID,
url: `${ResponseController.getBackendUrl()}/notifierServer/get/${sessionID}`,
notifierServer: `${ResponseController.getBackendUrl()}/notifierServer/get/${sessionID}`,
ws: `${ResponseController.getWebSocketUrl()}/notifierServer/getwebsocket/${sessionID}`
// url: `notifierServer/${sessionID}`, // url can be empty if we are using Web Sockets
ws: `${ResponseController.getWebSocketUrlWithoutWs()}`
}
}

Expand All @@ -138,8 +157,6 @@ class ResponseController
action: (url, info, sessionID) => {
return ResponseController.getBody({
"status": "ok",
"notifier": ResponseController.getNotifier(sessionID),
"notifierServer": ResponseController.getNotifier(sessionID).notifierServer
});
}
},
Expand Down Expand Up @@ -808,17 +825,25 @@ const QuestRoutes = [

]

const SITValidatorRoutes = [
const SITRoutes = [
{
url: "/client/sit-validator",
action: (url, info, sessionID) => {
return JSON.stringify(true);
}
},
{
url: "/client/WebSocketAddress",
action: (url, info, sessionID) => {
// const wss = global.WebSocketServer;
// return JSON.stringify(`ws://${wss.address().address}:${wss.address().port}`);
return ResponseController.getWebSocketUrl();
}
}

]

ResponseController.addRoutes(HideoutRoutes);
ResponseController.addRoutes(QuestRoutes);
ResponseController.addRoutes(RagfairRoutes);
ResponseController.addRoutes(SITValidatorRoutes);
ResponseController.addRoutes(SITRoutes);
2 changes: 0 additions & 2 deletions src/classes/notifier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";
const { Server } = require('./../../core/server/server')

/*
* NotifierService class maintains a queue of notifications which will be pushed upon notification
* request from client per session.
Expand Down
1 change: 0 additions & 1 deletion src/functions/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { Server } = require("../../core/server/server");
const { TarkovSend } = require("../../core/server/tarkovSend");
const { logger } = require("../../core/util/logger");
const fs = require('fs');
Expand Down
1 change: 0 additions & 1 deletion src/functions/response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { Server } = require("../../core/server/server");
const { logger } = require("../../core/util/logger");
const { AccountController } = require('../../src/Controllers/AccountController');
const { CustomizationController } = require("../Controllers/CustomizationController");
Expand Down

0 comments on commit 28d7e1b

Please sign in to comment.