Skip to content

Commit

Permalink
#368 Websockets use ROOT_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed May 1, 2023
1 parent 9ba5072 commit 971cd4c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion API/Backend/Config/routes/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ function openWebSocket(body, response, info, forceClientUpdate) {
}

const port = parseInt(process.env.PORT || "8888", 10);
const path = `ws://localhost:${port}/`;
const path = `ws://localhost:${port}${process.env.ROOT_PATH}/`;
const ws = new WebSocket(path);
ws.onopen = function () {
const data = {
Expand Down
21 changes: 20 additions & 1 deletion API/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const websocket = {
null,
""
);
const wss = new WebSocket.Server({ server });

const wss = new WebSocket.Server({ noServer: true });
websocket.wss = wss;

// Broadcast to all clients
Expand All @@ -42,6 +43,24 @@ const websocket = {
});
});

server.on("upgrade", function upgrade(request, socket, head) {
const pathname = request.url;
try {
if (
pathname ===
(process.env.ROOT_PATH || process.env.WEBSOCKET_ROOT_PATH || "") + "/"
) {
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit("connection", ws, request);
});
} else {
socket.destroy();
}
} catch (err) {
socket.destroy();
}
});

wss.on("close", () => {
logger("info", "Websocket disconnected...", "websocket", null, "");
websocket.wss = null;
Expand Down
9 changes: 6 additions & 3 deletions src/essence/essence.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ var essence = {

M.Toast.dismissAll()

const asMinutes = essence.webSocketRetryInterval / 60000 || ''
M.toast({
html: `Not connected to WebSocket. Will retry in ${(
essence.webSocketRetryInterval / 60000
).toFixed(2)} minutes...`,
html: `Not connected to WebSocket. Retrying in ${
asMinutes >= 1 ? parseInt(asMinutes) : asMinutes.toFixed(2)
} minute${asMinutes > 1 ? 's' : ''}...`,
displayLength: 10000,
classes: 'mmgisToast failure',
})
Expand All @@ -182,11 +183,13 @@ var essence = {
essence.webSocketRetryInterval >
essence.initialWebSocketRetryInterval
) {
/*
M.toast({
html: 'Successfully connected to WebSocket',
displayLength: 1600,
classes: 'mmgisToast',
})
*/

essence.webSocketRetryInterval =
essence.initialWebSocketRetryInterval
Expand Down

0 comments on commit 971cd4c

Please sign in to comment.