11import Gleap , { GleapFrameManager , GleapMetaDataManager , GleapSession } from "./Gleap" ;
22import { gleapDataParser } from "./GleapHelper" ;
33
4- const serverUrl = 'wss://ws.gleap.io' ;
5-
64export default class GleapStreamedEvent {
75 eventArray = [ ] ;
86 streamedEventArray = [ ] ;
@@ -14,109 +12,110 @@ export default class GleapStreamedEvent {
1412 socket = null ;
1513 connectedWebSocketGleapId = null ;
1614 connectionTimeout = null ;
15+ pingWS = null ;
16+ handleOpenBound = null ;
17+ handleErrorBound = null ;
18+ handleMessageBound = null ;
19+ handleCloseBound = null ;
20+
21+ // GleapStreamedEvent singleton
22+ static instance ;
23+ static getInstance ( ) {
24+ if ( ! this . instance ) {
25+ this . instance = new GleapStreamedEvent ( ) ;
26+ return this . instance ;
27+ } else {
28+ return this . instance ;
29+ }
30+ }
31+
32+ constructor ( ) {
33+ this . handleOpenBound = this . handleOpen . bind ( this ) ;
34+ this . handleErrorBound = this . handleError . bind ( this ) ;
35+ this . handleMessageBound = this . handleMessage . bind ( this ) ;
36+ this . handleCloseBound = this . handleClose . bind ( this ) ;
37+ }
1738
1839 cleanupWebSocket ( ) {
1940 if ( this . connectionTimeout ) {
2041 clearTimeout ( this . connectionTimeout ) ;
2142 this . connectionTimeout = null ;
2243 }
2344
45+ if ( this . pingWS ) {
46+ clearInterval ( this . pingWS ) ;
47+ }
48+
2449 if ( this . socket ) {
25- this . socket . onclose = null ;
26- this . socket . onerror = null ;
27- this . socket . onmessage = null ;
28- this . socket . onopen = null ;
50+ this . socket . removeEventListener ( 'open' , this . handleOpenBound ) ;
51+ this . socket . removeEventListener ( 'error' , this . handleErrorBound ) ;
52+ this . socket . removeEventListener ( 'message' , this . handleMessageBound ) ;
53+ this . socket . removeEventListener ( 'close' , this . handleCloseBound ) ;
2954 this . socket . close ( ) ;
3055 this . socket = null ;
3156 }
3257 }
3358
3459 initWebSocket ( ) {
35- const self = this ;
36- this . connectedWebSocketGleapId = GleapSession . getInstance ( ) . session . gleapId ;
60+ this . cleanupWebSocket ( ) ;
3761
38- console . log ( "Init websocket" ) ;
62+ this . connectedWebSocketGleapId = GleapSession . getInstance ( ) . session . gleapId ;
3963
4064 if ( ! GleapSession . getInstance ( ) . session || ! GleapSession . getInstance ( ) . sdkKey ) {
4165 return ;
4266 }
4367
44- this . socket = new WebSocket ( `${ serverUrl } ?gleapId=${ GleapSession . getInstance ( ) . session . gleapId } &gleapHash=${ GleapSession . getInstance ( ) . session . gleapHash } &apiKey=${ GleapSession . getInstance ( ) . sdkKey } &sdkVersion=${ SDK_VERSION } ` ) ;
45-
46- // Set a timeout for the connection to open
47- this . connectionTimeout = setTimeout ( ( ) => {
48- if ( self . socket . readyState !== self . socket . OPEN ) {
49- self . socket . close ( ) ;
50- console . error ( 'Connection timeout' ) ;
51-
52- GleapStreamedEvent . getInstance ( ) . initWebSocket ( ) ;
53- }
54- } , 5000 ) ; // Set timeout to 5 seconds
55-
56- // Event handler for the open event
57- this . socket . onopen = ( event ) => {
58- console . log ( 'Connected to the WebSocket server:' , event ) ;
68+ this . socket = new WebSocket ( `${ GleapSession . getInstance ( ) . wsApiUrl } ?gleapId=${ GleapSession . getInstance ( ) . session . gleapId } &gleapHash=${ GleapSession . getInstance ( ) . session . gleapHash } &apiKey=${ GleapSession . getInstance ( ) . sdkKey } &sdkVersion=${ SDK_VERSION } ` ) ;
69+ this . socket . addEventListener ( 'open' , this . handleOpenBound ) ;
70+ this . socket . addEventListener ( 'message' , this . handleMessageBound ) ;
71+ this . socket . addEventListener ( 'error' , this . handleErrorBound ) ;
72+ this . socket . addEventListener ( 'close' , this . handleCloseBound ) ;
73+ }
5974
60- // Clear the connection timeout as the connection is open
61- if ( self . connectionTimeout ) {
62- clearTimeout ( self . connectionTimeout ) ;
63- self . connectionTimeout = null ;
75+ handleOpen ( event ) {
76+ this . pingWS = setInterval ( ( ) => {
77+ if ( this . socket . readyState === this . socket . OPEN ) {
78+ this . socket . send ( JSON . stringify ( {
79+ name : 'ping' ,
80+ data : { } ,
81+ } ) ) ;
6482 }
65- } ;
83+ } , 30000 ) ;
6684
67- // Event handler for the message event to handle incoming messages
68- this . socket . onmessage = ( event ) => {
69- this . processMessage ( JSON . parse ( event . data ) ) ;
70- } ;
85+ if ( this . connectionTimeout ) {
86+ clearTimeout ( this . connectionTimeout ) ;
87+ this . connectionTimeout = null ;
88+ }
89+ }
7190
72- // Event handler for the error event
73- this . socket . onerror = ( error ) => {
74- console . error ( 'WebSocket Error:' , error ) ;
75- } ;
91+ handleMessage ( event ) {
92+ this . processMessage ( JSON . parse ( event . data ) ) ;
93+ }
7694
77- // Event handler for the close event
78- this . socket . onclose = ( event ) => {
79- // Check event.wasClean to see if the socket was closed cleanly
80- if ( event . wasClean ) {
81- console . log ( `Closed. Reason: ${ event . reason } Code: ${ event . code } ` ) ;
82- } else {
83- console . error ( `Connection died. Reason: ${ event . reason } Code: ${ event . code } ` ) ;
84- }
95+ handleError ( error ) { }
8596
86- // Attempt to reconnect after a delay
87- setTimeout ( ( ) => {
88- GleapStreamedEvent . getInstance ( ) . initWebSocket ( ) ;
89- } , 5000 ) ;
90- } ;
97+ handleClose ( event ) {
98+ setTimeout ( ( ) => {
99+ this . initWebSocket ( ) ;
100+ } , 5000 ) ;
91101 }
92102
93103 processMessage ( message ) {
94104 try {
95- const { a, u } = message ;
96- if ( ! GleapFrameManager . getInstance ( ) . isOpened ( ) ) {
97- if ( a ) {
98- Gleap . getInstance ( ) . performActions ( a ) ;
99- }
100- if ( u != null ) {
101- GleapNotificationManager . getInstance ( ) . setNotificationCount ( u ) ;
105+ if ( message . name === 'update' ) {
106+ const { a, u } = message . data ;
107+ if ( ! GleapFrameManager . getInstance ( ) . isOpened ( ) ) {
108+ if ( a ) {
109+ Gleap . getInstance ( ) . performActions ( a ) ;
110+ }
111+ if ( u != null ) {
112+ GleapNotificationManager . getInstance ( ) . setNotificationCount ( u ) ;
113+ }
102114 }
103115 }
104116 } catch ( exp ) { }
105117 }
106118
107- // GleapStreamedEvent singleton
108- static instance ;
109- static getInstance ( ) {
110- if ( ! this . instance ) {
111- this . instance = new GleapStreamedEvent ( ) ;
112- return this . instance ;
113- } else {
114- return this . instance ;
115- }
116- }
117-
118- constructor ( ) { }
119-
120119 getEventArray ( ) {
121120 return this . eventArray ;
122121 }
@@ -141,7 +140,6 @@ export default class GleapStreamedEvent {
141140 restart ( ) {
142141 // Only reconnect websockets when needed.
143142 if ( this . connectedWebSocketGleapId !== GleapSession . getInstance ( ) . session . gleapId ) {
144- this . cleanupWebSocket ( ) ;
145143 this . initWebSocket ( ) ;
146144 }
147145
@@ -214,27 +212,22 @@ export default class GleapStreamedEvent {
214212
215213 streamEvents = ( ) => {
216214 if ( ! GleapSession . getInstance ( ) . ready || this . streamingEvents || this . errorCount > 2 ) {
217- console . log ( "Not ready to stream events" ) ;
218215 return ;
219216 }
220217
221218 // Nothing to stream.
222219 if ( this . streamedEventArray . length === 0 ) {
223- console . log ( "Nothing to stream" ) ;
224220 return ;
225221 }
226222
227223 // Sockets not connected.
228224 if ( ! this . socket || this . socket . readyState !== this . socket . OPEN ) {
229- console . log ( "Socket not connected" ) ;
230225 return ;
231226 }
232227
233228 const self = this ;
234229 this . streamingEvents = true ;
235230
236- console . log ( this . streamedEventArray ) ;
237-
238231 const http = new XMLHttpRequest ( ) ;
239232 http . open ( "POST" , GleapSession . getInstance ( ) . apiUrl + "/sessions/ping" ) ;
240233 http . setRequestHeader ( "Content-Type" , "application/json;charset=UTF-8" ) ;
0 commit comments