@@ -2,7 +2,7 @@ import { AxiosResponse } from "axios";
22import axios from 'axios' ;
33import { Channel } from "./Channel" ;
44
5- export type Options = { authEndpoint : string , host : string } ;
5+ export type Options = { authEndpoint : string , host : string , debug : boolean } ;
66export type MessageBody = { event : string , channel ?: string , data : object } ;
77
88export class Websocket {
@@ -25,6 +25,7 @@ export class Websocket {
2525 private pingInterval : NodeJS . Timeout ;
2626
2727 private connect ( host : string ) : void {
28+ this . options . debug && console . log ( 'Connecting' ) ;
2829
2930 this . websocket = new WebSocket ( host )
3031
@@ -50,6 +51,8 @@ export class Websocket {
5051 }
5152
5253 if ( message . channel ) {
54+ this . options . debug && console . log ( `Received event ${ message . event } on channel ${ message . channel } ` )
55+
5356 if ( this . listeners [ message . channel ] && this . listeners [ message . channel ] [ message . event ] ) {
5457 this . listeners [ message . channel ] [ message . event ] ( message . data )
5558 }
@@ -66,6 +69,7 @@ export class Websocket {
6669
6770 this . websocket . onclose = ( ) => {
6871 if ( this . socketId && ! this . closing || ! this . socketId ) {
72+ this . options . debug && console . info ( 'Connection lost, reconnecting...' ) ;
6973 setTimeout ( ( ) => {
7074 this . socketId = undefined
7175 this . connect ( host )
@@ -76,6 +80,8 @@ export class Websocket {
7680 this . on ( 'whoami' , ( { socket_id : socketId } ) => {
7781 this . socketId = socketId
7882
83+ this . options . debug && console . log ( `just set socketId to ${ socketId } ` )
84+
7985 while ( this . channelBacklog . length ) {
8086 const channel = this . channelBacklog [ 0 ]
8187
@@ -89,6 +95,7 @@ export class Websocket {
8995 // send ping every 60 seconds to keep connection alive
9096 this . pingInterval = setInterval ( ( ) => {
9197 if ( this . websocket . readyState === this . websocket . OPEN ) {
98+ this . options . debug && console . log ( 'Sending ping' )
9299 this . send ( {
93100 event : 'ping' ,
94101 } )
@@ -109,7 +116,7 @@ export class Websocket {
109116 try {
110117 return JSON . parse ( body )
111118 } catch ( error ) {
112- console . error ( 'Error parsing message' , error )
119+ this . options . debug && console . error ( error )
113120
114121 return undefined
115122 }
@@ -152,11 +159,13 @@ export class Websocket {
152159
153160 private actuallySubscribe ( channel : Channel ) : void {
154161 if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
162+ this . options . debug && console . log ( `Sending auth request for channel ${ channel . name } ` )
155163
156164 axios . post ( this . options . authEndpoint , {
157165 socket_id : this . getSocketId ( ) ,
158166 channel_name : channel . name ,
159167 } ) . then ( ( response : AxiosResponse ) => {
168+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
160169
161170 this . send ( {
162171 event : 'subscribe' ,
@@ -166,9 +175,11 @@ export class Websocket {
166175 } ,
167176 } )
168177 } ) . 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 )
170180 } )
171181 } else {
182+ this . options . debug && console . log ( `Subscribing to channels ${ channel . name } ` )
172183
173184 this . send ( {
174185 event : 'subscribe' ,
0 commit comments