@@ -5,7 +5,7 @@ const connection = new WebSocket(`wss://ws.binaryws.com/websockets/v3?app_id=${a
55
66const api = new DerivAPIBasic ( { connection } ) ;
77
8- const keepAlive = ( ) => {
8+ const proposal = ( ) => {
99 api . subscribe ( {
1010 proposal : 1 ,
1111 subscribe : 1 ,
@@ -18,17 +18,21 @@ const keepAlive = () => {
1818 symbol : 'R_100' ,
1919 barrier : '+0.1' ,
2020 } ) ;
21- /*
22- * Send a ping ever 30 seconds to keep the connection alive, needs to use the same
23- * websocket connection as the one you want to maintain.
24- */
2521} ;
2622
27- const keepAliveRes = async ( res ) => {
23+ // Send a ping every 30 seconds to keep the connection alive
24+ // Needs to use the same websocket connection as the one you want to maintain.
25+ const ping = ( ) => {
26+ setInterval ( ( ) => {
27+ api . ping ( ) ;
28+ } , 30000 ) ;
29+ } ;
30+
31+ const wsResponse = async ( res ) => {
2832 const data = JSON . parse ( res . data ) ;
2933 if ( data . error !== undefined ) {
3034 console . log ( 'Error: %s ' , data . error . message ) ;
31- connection . removeEventListener ( 'message' , keepAliveRes , false ) ;
35+ connection . removeEventListener ( 'message' , wsResponse , false ) ;
3236 await api . disconnect ( ) ;
3337 } else if ( data . msg_type === 'proposal' ) {
3438 console . log ( 'Details: %s' , data . proposal . longcode ) ;
@@ -40,14 +44,15 @@ const keepAliveRes = async (res) => {
4044 }
4145} ;
4246
43- const checkSignal = async ( ) => {
44- await keepAlive ( ) ;
45- connection . addEventListener ( 'message' , keepAliveRes ) ;
47+ const checkSignal = ( ) => {
48+ proposal ( ) ;
49+ ping ( ) ;
50+ connection . addEventListener ( 'message' , wsResponse ) ;
4651} ;
4752
4853const endCall = ( ) => {
49- connection . removeEventListener ( 'message' , keepAliveRes , false ) ;
50- keepAlive ( ) . unsubscribe ( ) ;
54+ connection . removeEventListener ( 'message' , wsResponse , false ) ;
55+ proposal ( ) . unsubscribe ( ) ;
5156} ;
5257
5358const keep_alive_button = document . querySelector ( '#keep_alive' ) ;
0 commit comments