From 6a592e34791396909b1befea9e6f6e5051c30d43 Mon Sep 17 00:00:00 2001 From: Alessandro Toppi Date: Tue, 23 Nov 2021 15:17:09 +0100 Subject: [PATCH] Improve helloworld example --- examples/helloworld/src/index.js | 34 ++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/examples/helloworld/src/index.js b/examples/helloworld/src/index.js index eaa0e16..03d5d06 100644 --- a/examples/helloworld/src/index.js +++ b/examples/helloworld/src/index.js @@ -11,6 +11,7 @@ const closeAfterSecs = 120; (async function startConnection() { let connection = null; + let endTask; try { // Janode is a connection factory with Promise API @@ -20,14 +21,18 @@ const closeAfterSecs = 120; // close connection after X seconds Logger.info(`${LOG_NS} auto-destroying janus connection in ${closeAfterSecs} seconds`); - setTimeout(() => connection.close().catch(() => { }).then(() => Logger.info(`${LOG_NS} janus connection CLOSED`)), closeAfterSecs * 1000); + endTask = setTimeout(() => connection.close().catch(() => { }).then(() => Logger.info(`${LOG_NS} janus connection CLOSED`)), closeAfterSecs * 1000); // connection closing (Sessions -> WS closed -> connection closed) - connection.on(Janode.EVENT.CONNECTION_CLOSED, () => Logger.info(`${LOG_NS} ***** CONNECTION CLOSED *****`)); + connection.on(Janode.EVENT.CONNECTION_CLOSED, () => { + Logger.info(`${LOG_NS} ***** CONNECTION CLOSED *****`); + clearTimeout(endTask); + }); // connection error event (i.e. WS error, unexpected WS close) connection.on(Janode.EVENT.CONNECTION_ERROR, ({ message }) => { Logger.error(`${LOG_NS} xxxxx CONNECTION ERROR xxxxx (${message})`); + clearTimeout(endTask); }); // Connection API: Janus GET INFO @@ -35,7 +40,7 @@ const closeAfterSecs = 120; Logger.info(`${LOG_NS} ***** GET INFO REQ OK ***** server = ${info.name} ${info.version_string}`); // Connection API: Janus CREATE SESSION - const session = await connection.create(); + const session = await connection.create(10); Logger.info(`${LOG_NS} ***** SESSION CREATED *****`); @@ -56,6 +61,7 @@ const closeAfterSecs = 120; } catch ({ message }) { Logger.error(`${LOG_NS} xxxxx JANODE SETUP ERROR xxxxx (${message})`); if (connection) connection.close().catch(() => { }); + clearTimeout(endTask); } })(); @@ -63,13 +69,27 @@ const closeAfterSecs = 120; (async function startAdmin() { let admin = null; - let task = null; + let task, endTask = null; try { admin = await Janode.connect(janodeConfig, 'janus_1_admin'); Logger.info(`${LOG_NS} ***** ADMIN CONNECTION CREATED *****`); + // connection closing (Sessions -> WS closed -> connection closed) + admin.on(Janode.EVENT.CONNECTION_CLOSED, () => { + Logger.info(`${LOG_NS} ***** ADMIN CONNECTION CLOSED *****`); + clearInterval(task); + clearTimeout(endTask); + }); + + // connection error event (i.e. WS error, unexpected WS close) + admin.on(Janode.EVENT.CONNECTION_ERROR, ({ message }) => { + Logger.error(`${LOG_NS} xxxxx ADMIN CONNECTION ERROR xxxxx (${message})`); + clearInterval(task); + clearTimeout(endTask); + }); + task = setInterval(async () => { try { const data = await admin.listSessions(); @@ -81,9 +101,10 @@ const closeAfterSecs = 120; // close connection after X seconds Logger.info(`${LOG_NS} auto-destroying admin connection in ${closeAfterSecs} seconds`); - setTimeout(() => { + endTask = setTimeout(() => { admin.close().catch(() => { }).then(() => Logger.info(`${LOG_NS} admin connection CLOSED`)); clearInterval(task); + clearTimeout(endTask); }, closeAfterSecs * 1000); // Connection API: Janus GET INFO @@ -94,6 +115,7 @@ const closeAfterSecs = 120; Logger.info(`${LOG_NS} xxxxx JANODE ADMIN STUP ERROR xxxxx ${message}`); if (admin) admin.close().catch(() => { }); clearInterval(task); + clearTimeout(endTask); } -})(); \ No newline at end of file +}); \ No newline at end of file