@@ -2,7 +2,7 @@ import { AxiosResponse } from "axios";
2
2
import axios from 'axios' ;
3
3
import { Channel } from "./Channel" ;
4
4
5
- export type Options = { authEndpoint : string , host : string } ;
5
+ export type Options = { authEndpoint : string , host : string , debug : boolean } ;
6
6
export type MessageBody = { event : string , channel ?: string , data : object } ;
7
7
8
8
export class Websocket {
@@ -25,6 +25,7 @@ export class Websocket {
25
25
private pingInterval : NodeJS . Timeout ;
26
26
27
27
private connect ( host : string ) : void {
28
+ this . options . debug && console . log ( 'Connecting' ) ;
28
29
29
30
this . websocket = new WebSocket ( host )
30
31
@@ -50,6 +51,8 @@ export class Websocket {
50
51
}
51
52
52
53
if ( message . channel ) {
54
+ this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` )
55
+
53
56
if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
54
57
this . listeners [ message . channel ] [ message . event ] ( message . data )
55
58
}
@@ -66,6 +69,7 @@ export class Websocket {
66
69
67
70
this . websocket . onclose = ( ) => {
68
71
if ( this . socketId && ! this . closing || ! this . socketId ) {
72
+ this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
69
73
setTimeout ( ( ) => {
70
74
this . socketId = undefined
71
75
this . connect ( host )
@@ -76,6 +80,8 @@ export class Websocket {
76
80
this . on ( 'whoami' , ( { socket_id : socketId } ) => {
77
81
this . socketId = socketId
78
82
83
+ this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
84
+
79
85
while ( this . channelBacklog . length ) {
80
86
const channel = this . channelBacklog [ 0 ]
81
87
@@ -89,6 +95,7 @@ export class Websocket {
89
95
// send ping every 60 seconds to keep connection alive
90
96
this . pingInterval = setInterval ( ( ) => {
91
97
if ( this . websocket . readyState === this . websocket . OPEN ) {
98
+ this . options . debug && console . log ( 'Sending ping' )
92
99
this . send ( {
93
100
event : 'ping' ,
94
101
} )
@@ -109,7 +116,7 @@ export class Websocket {
109
116
try {
110
117
return JSON . parse ( body )
111
118
} catch ( error ) {
112
- console . error ( 'Error parsing message' , error )
119
+ this . options . debug && console . error ( error )
113
120
114
121
return undefined
115
122
}
@@ -152,11 +159,13 @@ export class Websocket {
152
159
153
160
private actuallySubscribe ( channel : Channel ) : void {
154
161
if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
162
+ this . options . debug && console . log ( `Sending auth request for channel ${ channel . name } ` )
155
163
156
164
axios . post ( this . options . authEndpoint , {
157
165
socket_id : this . getSocketId ( ) ,
158
166
channel_name : channel . name ,
159
167
} ) . then ( ( response : AxiosResponse ) => {
168
+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
160
169
161
170
this . send ( {
162
171
event : 'subscribe' ,
@@ -166,9 +175,11 @@ export class Websocket {
166
175
} ,
167
176
} )
168
177
} ) . catch ( ( error ) => {
169
- console . error ( 'Error while subscribing to private channel :' , error )
178
+ this . options . debug && console . log ( `Auth request for channel ${ channel . name } failed` )
179
+ this . options . debug && console . error ( error )
170
180
} )
171
181
} else {
182
+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
172
183
173
184
this . send ( {
174
185
event : 'subscribe' ,
0 commit comments