From 690d42251d78dc6b6e1d0e2bd49a7430e7863218 Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Tue, 6 Jun 2023 12:59:10 +0200 Subject: [PATCH 1/3] chore: refactoring keep alive example --- examples/keep_alive/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/keep_alive/index.js b/examples/keep_alive/index.js index 4227d3840..a99bbf06c 100644 --- a/examples/keep_alive/index.js +++ b/examples/keep_alive/index.js @@ -18,10 +18,14 @@ const keepAlive = () => { symbol: 'R_100', barrier: '+0.1', }); - /* - * Send a ping ever 30 seconds to keep the connection alive, needs to use the same - * websocket connection as the one you want to maintain. - */ +}; + +// Send a ping ever 30 seconds to keep the connection alive, needs to use the same +// websocket connection as the one you want to maintain. +const ping = () => { + setInterval(() => { + api.ping(); + }, 30000); }; const keepAliveRes = async (res) => { @@ -40,8 +44,9 @@ const keepAliveRes = async (res) => { } }; -const checkSignal = async () => { - await keepAlive(); +const checkSignal = () => { + keepAlive(); + ping(); connection.addEventListener('message', keepAliveRes); }; From 87eeb1ff85a002e7147e130bdb61ffdeba0054a1 Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Tue, 6 Jun 2023 13:03:52 +0200 Subject: [PATCH 2/3] chore: refactoring more --- examples/keep_alive/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/keep_alive/index.js b/examples/keep_alive/index.js index a99bbf06c..2a72fa0f0 100644 --- a/examples/keep_alive/index.js +++ b/examples/keep_alive/index.js @@ -5,7 +5,7 @@ const connection = new WebSocket(`wss://ws.binaryws.com/websockets/v3?app_id=${a const api = new DerivAPIBasic({ connection }); -const keepAlive = () => { +const proposal = () => { api.subscribe({ proposal: 1, subscribe: 1, @@ -20,15 +20,15 @@ const keepAlive = () => { }); }; -// Send a ping ever 30 seconds to keep the connection alive, needs to use the same -// websocket connection as the one you want to maintain. +// Send a ping every 30 seconds to keep the connection alive +// Needs to use the same websocket connection as the one you want to maintain. const ping = () => { setInterval(() => { api.ping(); }, 30000); }; -const keepAliveRes = async (res) => { +const wsResponse = async (res) => { const data = JSON.parse(res.data); if (data.error !== undefined) { console.log('Error: %s ', data.error.message); @@ -45,14 +45,14 @@ const keepAliveRes = async (res) => { }; const checkSignal = () => { - keepAlive(); + proposal(); ping(); - connection.addEventListener('message', keepAliveRes); + connection.addEventListener('message', wsResponse); }; const endCall = () => { - connection.removeEventListener('message', keepAliveRes, false); - keepAlive().unsubscribe(); + connection.removeEventListener('message', wsResponse, false); + proposal().unsubscribe(); }; const keep_alive_button = document.querySelector('#keep_alive'); From 2bdd546a6b1b59ab6c922e45c7b90ffdaf99e98f Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Tue, 6 Jun 2023 13:04:51 +0200 Subject: [PATCH 3/3] chore: refactoring more --- examples/keep_alive/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/keep_alive/index.js b/examples/keep_alive/index.js index 2a72fa0f0..2bdc43950 100644 --- a/examples/keep_alive/index.js +++ b/examples/keep_alive/index.js @@ -32,7 +32,7 @@ const wsResponse = async (res) => { const data = JSON.parse(res.data); if (data.error !== undefined) { console.log('Error: %s ', data.error.message); - connection.removeEventListener('message', keepAliveRes, false); + connection.removeEventListener('message', wsResponse, false); await api.disconnect(); } else if (data.msg_type === 'proposal') { console.log('Details: %s', data.proposal.longcode);