Skip to content

Commit

Permalink
Fixed reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeue committed Aug 19, 2023
1 parent 48ba922 commit 3328ca8
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homestudio-remote",
"version": "1.7.1",
"version": "1.7.2",
"description": "Homestudio for on-site production",
"main": "server.js",
"scripts": {
Expand Down
124 changes: 79 additions & 45 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ let configLoaded = false;
config.info('omeDocker', `If setup of OME has failed try using this command manually in your command prompt: <code class="bg-secondary card d-block m-1 my-3 p-1 px-2 text-light position-static">${dockerCommand}</code>`, ['omeType', 'docker']);
}
config.require('host', [], 'What is the IP/host of Oven Media Engine? (normally this machines IP)');
config.require('port', [], 'What port shall the server use?');
config.require('systemName', [], 'What is the name of the system/job?');
config.require('port', [], 'What port shall the server use');
config.require('systemName', [], 'What is the name of the system/job');
config.require('reconnectTimeoutSeconds', [], 'How long should a stream wait before trying to reconnect in the GUI');
config.require('loggingLevel', {'A':'All', 'D':'Debug', 'W':'Warnings', 'E':'Errors'}, 'Set logging level:');
config.require('createLogFile', {true: 'Yes', false: 'No'}, 'Save logs to local file?');
config.require('advancedConfig', {true: 'Yes', false: 'No'}, 'Show advanced config settings?');
config.require('createLogFile', {true: 'Yes', false: 'No'}, 'Save logs to local file');
config.require('advancedConfig', {true: 'Yes', false: 'No'}, 'Show advanced config settings');
{
config.require('debugLineNum', {true: 'Yes', false: 'No'}, 'Print line numbers?', ['advancedConfig', true]);
config.require('printPings', {true: 'Yes', false: 'No'}, 'Print pings?', ['advancedConfig', true]);
Expand All @@ -73,6 +74,7 @@ let configLoaded = false;
config.default('homestudioKey', '');
config.default('omeType', 'docker');
config.default('host', 'localhost');
config.default('reconnectTimeoutSeconds', 4);


if (!await config.fromFile(path.join(__data, 'HomeStudioData', 'config.conf'))) {
Expand Down Expand Up @@ -359,6 +361,7 @@ function expressRoutes(expressApp) {
decoders: decoders(),
host: config.get('host'),
dockerCommand: dockerCommand,
reconnectTimeoutSeconds: config.get('reconnectTimeoutSeconds'),
config: false
});
});
Expand All @@ -373,6 +376,7 @@ function expressRoutes(expressApp) {
decoders: decoders(),
host: config.get('host'),
dockerCommand: dockerCommand,
reconnectTimeoutSeconds: config.get('reconnectTimeoutSeconds'),
config: true
});
});
Expand Down Expand Up @@ -585,55 +589,75 @@ async function startPush(id) {
"url": decoderConfig.URL+"?mode=caller"
}
logs.debug(`Sending push request to: http://${config.get('host')}:8081/v1/vhosts/default/apps/app:startPush`);
const response = await fetch(`http://${config.get('host')}:8081/v1/vhosts/default/apps/app:startPush`,{
method: 'POST',
headers: {"Authorization": "Basic "+Buffer.from("admin:NEPVisions!").toString('base64')},
body: JSON.stringify(body)
})
const jsonRpcResponse = await response.json();
if (response.status !== 200) {
logs.error('Could not reach OME server', response.statusText);
try {
const response = await fetch(`http://${config.get('host')}:8081/v1/vhosts/default/apps/app:startPush`,{
method: 'POST',
headers: {"Authorization": "Basic "+Buffer.from("admin:NEPVisions!").toString('base64')},
body: JSON.stringify(body)
})
const jsonRpcResponse = await response.json();
if (response.status !== 200) {
logs.error('Could not reach OME server', response.statusText);
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error pushing stream: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return;
}
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error pushing stream: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
"message": `Started pushing stream: <pre <pre class="d-none" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return jsonRpcResponse;
} catch (error) {
logs.error('Could not reach OME server', error);
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error pushing stream: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(error, null, 4)}</pre>`
});
return;
}
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Started pushing stream <pre <pre class="d-none" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return jsonRpcResponse;
}

async function stopPush(id) {
const body = {
"id": "push_decoder_"+id
}
logs.debug(`Sending push request to: http://${config.get('host')}:8081/v1/vhosts/default/apps/app:stopPush`);
const response = await fetch(`http://${config.get('host')}:8081/v1/vhosts/default/apps/app:stopPush`,{
method: 'POST',
headers: {"Authorization": "Basic "+Buffer.from("admin:NEPVisions!").toString('base64')},
body: JSON.stringify(body)
})
const jsonRpcResponse = await response.json();
if (response.status !== 200) {
logs.error('Could not reach OME server', response.statusText);
try {
const response = await fetch(`http://${config.get('host')}:8081/v1/vhosts/default/apps/app:stopPush`,{
method: 'POST',
headers: {"Authorization": "Basic "+Buffer.from("admin:NEPVisions!").toString('base64')},
body: JSON.stringify(body)
})
const jsonRpcResponse = await response.json();
if (response.status !== 200) {
logs.error('Could not reach OME server', response.statusText);
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error stopping stream: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return;
}
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error stopping stream: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
"message": `Stopped pushing stream: <pre class="d-none" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return jsonRpcResponse;
} catch (error) {
logs.error('Could not reach OME server', error);
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error stopping stream: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(error, null, 4)}</pre>`
});
return;
}
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Stopped pushing stream <pre class="d-none" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return jsonRpcResponse;
}

async function getPush(id) {
Expand All @@ -643,21 +667,31 @@ async function getPush(id) {
}
if (id) postOptions.body = JSON.stringify({"id": "push_decoder_"+id});
logs.debug(`Getting pushes from to: http://${config.get('host')}:8081/v1/vhosts/default/apps/app:pushes`);
const response = await fetch(`http://${config.get('host')}:8081/v1/vhosts/default/apps/app:pushes`, postOptions)
const jsonRpcResponse = await response.json();
if (response.status !== 200) {
logs.error('Could not reach OME server', response.statusText);
try {
const response = await fetch(`http://${config.get('host')}:8081/v1/vhosts/default/apps/app:pushes`, postOptions)
const jsonRpcResponse = await response.json();
if (response.status !== 200) {
logs.error('Could not reach OME server', response.statusText);
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error getting pushes: ${response.statusText}: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
});
return;
}
webServer.sendToAll({
"command": "log",
"type": "pushStatus",
"message": jsonRpcResponse
});
return jsonRpcResponse;
} catch (error) {
logs.error('Could not reach OME server', error);
webServer.sendToAll({
"command": "log",
"type": "decoding",
"message": `Error getting pushes: ${response.statusText}, <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(jsonRpcResponse, null, 4)}</pre>`
"message": `Error getting pushes cannot connect to server: <pre class="bg-secondary card p-1 px-2 mt-2" style="white-space: break-spaces;">${JSON.stringify(error, null, 4)}</pre>`
});
return;
}
webServer.sendToAll({
"command": "log",
"type": "pushStatus",
"message": jsonRpcResponse
});
return jsonRpcResponse;
}
2 changes: 1 addition & 1 deletion static/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ function openPlayer($element) {
delete player.timeout;
if (player.getState() === 'playing') return;
player.load();
}, 2000);
}, 1000 * reconnectTimeoutSeconds);
break;
}
})
Expand Down
35 changes: 30 additions & 5 deletions static/lib/commonWS.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class webSocket extends EventTarget {
this.protocol = 'ws';
this.currentSystem = currentSystem;
this.logger = logger;
this.timeout;
this.timeoutCounter = 0;

if (ssl) {
this.protocol = 'wss';
Expand All @@ -23,20 +25,40 @@ class webSocket extends EventTarget {
}

connect(serverURL) {
clearTimeout(this.timeout);
if (this.serverURL !== serverURL) {
if (typeof this.server !== 'undefined') {
this.server.disconnect();
this.server.close();
}
this.server = null;
this.serverURL = serverURL;
this.connecting = false;
}
if (this.connecting) return this.server;
if (this.connecting) {
if (this.timeoutCounter > 4) {
this.timeoutCounter = 0;
this.logger.log('Terminating hung connection');
this.server.close();
this.server = null;
this.serverURL = serverURL;
this.connecting = false;
this.connect(this.serverURL);
} else {
this.timeoutCounter++;
this.timeout = setTimeout(() => {
this.logger.log('Waiting for connection');
this.connect(this.serverURL);
}, 500);
return this.server;
}
}
this.connecting = true;
this.logger.log(`Connecting to: ${this.protocol}://${this.serverURL}`);
this.server = new WebSocket(`${this.protocol}://${this.serverURL}`);

this.server.onopen = event => {
clearTimeout(this.timeout);
this.timeoutCounter = 0;
this.logger.log('Connection established!');
this.connecting = false;
this.dispatchEvent(new Event('open'));
Expand All @@ -58,11 +80,14 @@ class webSocket extends EventTarget {
};

this.server.onclose = () => {
if (!this.forceShut) {
if (this.forceShut) {
this.logger.log('Connection forcably ended');
} else {
this.logger.log('Connection ended');
setTimeout(() => {
this.timeout = setTimeout(() => {
this.logger.log('Reconnecting');
this.connect(this.serverURL);
}, 500);
}, 2000);
}
this.forceShut = false;
this.dispatchEvent(new Event('close'));
Expand Down
2 changes: 1 addition & 1 deletion views/appHome.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
</div>
</section>

<div class="mt-3 mx-0 align-self-end d-flex gap-1">
<div class="mt-3 mx-0 align-self-end d-flex gap-1 flex-wrap">
<input id="csvUpload" class="form-control" type="file" accept="text/csv" name="csvUpload" size="30">
<button class="btn btn-primary tableImport" type="button" disabled>Import CSV</button>
<button class="btn btn-primary tableExport" type="button">Export as CSV</button>
Expand Down
3 changes: 2 additions & 1 deletion views/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const host = '<%=host%>';
let encoders = <%- JSON.stringify(encoders) %>;
let decoders = <%- JSON.stringify(decoders) %>;
const reconnectTimeoutSeconds = <%- reconnectTimeoutSeconds %>;
window.encoders = encoders;
window.decoders = decoders;
</script>
Expand Down Expand Up @@ -181,7 +182,7 @@
</div>
</section>

<div class="mt-3 mx-0 align-self-end d-flex gap-1">
<div class="mt-3 mx-0 align-self-end d-flex gap-1 flex-wrap">
<input id="csvUpload" class="form-control" type="file" accept="text/csv" name="csvUpload" size="30">
<button class="btn btn-primary tableImport" type="button" disabled>Import CSV</button>
<button class="btn btn-primary tableExport" type="button">Export as CSV</button>
Expand Down

0 comments on commit 3328ca8

Please sign in to comment.