@@ -21,15 +21,27 @@ export class Websocket {
21
21
private socketId : string ;
22
22
23
23
private closing = false ;
24
+ private hasConnected = false ;
24
25
25
26
private pingInterval : NodeJS . Timeout ;
26
27
27
28
private connect ( host : string ) : void {
28
- this . options . debug && console . log ( 'Connecting ' ) ;
29
+ this . options . debug && console . log ( 'Trying to connect... ' ) ;
29
30
30
31
this . websocket = new WebSocket ( host )
31
32
33
+ this . websocket . onerror = ( ) => {
34
+ if ( ! this . hasConnected ) {
35
+ setTimeout ( ( ) => {
36
+ this . socketId = undefined
37
+ this . connect ( host )
38
+ } , 3000 ) ;
39
+ }
40
+ }
41
+
32
42
this . websocket . onopen = ( ) => {
43
+ this . options . debug && console . log ( 'Connected !' ) ;
44
+ this . hasConnected = true ;
33
45
this . send ( {
34
46
event : 'whoami' ,
35
47
} )
@@ -41,66 +53,71 @@ export class Websocket {
41
53
42
54
this . buffer . splice ( 0 , 1 )
43
55
}
44
- }
45
56
46
- this . websocket . onmessage = ( messageEvent : MessageEvent ) => {
47
- const message = this . parseMessage ( messageEvent . data )
57
+ // Register events only once connected, or they won't be registered if connection failed/lost
48
58
49
- if ( ! message ) {
50
- return
51
- }
59
+ this . websocket . onmessage = ( messageEvent : MessageEvent ) => {
60
+ const message = this . parseMessage ( messageEvent . data )
61
+ this . options . debug && console . log ( 'onmessage' , messageEvent . data )
62
+
63
+ if ( ! message ) {
64
+ return
65
+ }
66
+
67
+ if ( message . channel ) {
68
+ this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` )
52
69
53
- if ( message . channel ) {
54
- this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` )
70
+ if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
71
+ this . listeners [ message . channel ] [ message . event ] ( message . data )
72
+ }
55
73
56
- if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
57
- this . listeners [ message . channel ] [ message . event ] ( message . data )
74
+ return
58
75
}
59
76
60
- return
77
+ if ( this . internalListeners [ message . event ] ) {
78
+ this . internalListeners [ message . event ] ( message . data )
79
+ }
61
80
}
62
81
63
- if ( this . internalListeners [ message . event ] ) {
64
- this . internalListeners [ message . event ] ( message . data )
65
- }
66
82
83
+ // send ping every 60 seconds to keep connection alive
84
+ this . pingInterval = setInterval ( ( ) => {
85
+ if ( this . websocket . readyState === this . websocket . OPEN ) {
86
+ this . options . debug && console . log ( 'Sending ping' )
87
+ this . send ( {
88
+ event : 'ping' ,
89
+ } )
90
+ }
91
+ } , 60 * 1000 )
67
92
}
68
93
69
94
70
95
this . websocket . onclose = ( ) => {
71
- if ( this . socketId && ! this . closing || ! this . socketId ) {
72
- this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
73
- setTimeout ( ( ) => {
74
- this . socketId = undefined
75
- this . connect ( host )
76
- } , 1000 ) ;
96
+ this . options . debug && console . info ( 'Connection closed.' ) ;
97
+ if ( this . closing ) {
98
+ return ;
77
99
}
100
+ this . hasConnected = false
101
+ this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
102
+ setTimeout ( ( ) => {
103
+ this . socketId = undefined
104
+ this . connect ( host )
105
+ } , 1000 ) ;
78
106
} ;
79
107
80
108
this . on ( 'whoami' , ( { socket_id : socketId } ) => {
81
109
this . socketId = socketId
82
110
83
111
this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
84
112
85
- while ( this . channelBacklog . length ) {
86
- const channel = this . channelBacklog [ 0 ]
87
-
113
+ // Handle the backlog and don't empty it, we'll need it if we lose connection
114
+ let channel : Channel ;
115
+ for ( channel of this . channelBacklog ) {
88
116
this . actuallySubscribe ( channel )
89
-
90
- this . channelBacklog . splice ( 0 , 1 )
91
117
}
92
- } )
93
118
119
+ } )
94
120
95
- // send ping every 60 seconds to keep connection alive
96
- this . pingInterval = setInterval ( ( ) => {
97
- if ( this . websocket . readyState === this . websocket . OPEN ) {
98
- this . options . debug && console . log ( 'Sending ping' )
99
- this . send ( {
100
- event : 'ping' ,
101
- } )
102
- }
103
- } , 60 * 1000 )
104
121
105
122
}
106
123
0 commit comments