@@ -23,9 +23,9 @@ export class Websocket extends EventEmitter {
2323 private token : string = '' ;
2424
2525 // Connects to the websocket instance and sets the token for the initial request.
26- connect ( url : string ) {
26+ connect ( url : string ) : this {
2727 this . url = url ;
28- this . socket = new Sockette ( `${ this . url } ?token= ${ this . token } ` , {
28+ this . socket = new Sockette ( `${ this . url } ` , {
2929 onmessage : e => {
3030 try {
3131 let { event, args } = JSON . parse ( e . data ) ;
@@ -34,11 +34,19 @@ export class Websocket extends EventEmitter {
3434 console . warn ( 'Failed to parse incoming websocket message.' , ex ) ;
3535 }
3636 } ,
37- onopen : ( ) => this . emit ( 'SOCKET_OPEN' ) ,
38- onreconnect : ( ) => this . emit ( 'SOCKET_RECONNECT' ) ,
37+ onopen : ( ) => {
38+ this . emit ( 'SOCKET_OPEN' ) ;
39+ this . authenticate ( ) ;
40+ } ,
41+ onreconnect : ( ) => {
42+ this . emit ( 'SOCKET_RECONNECT' ) ;
43+ this . authenticate ( ) ;
44+ } ,
3945 onclose : ( ) => this . emit ( 'SOCKET_CLOSE' ) ,
4046 onerror : ( ) => this . emit ( 'SOCKET_ERROR' ) ,
4147 } ) ;
48+
49+ return this ;
4250 }
4351
4452 // Returns the URL connected to for the socket.
@@ -48,11 +56,11 @@ export class Websocket extends EventEmitter {
4856
4957 // Sets the authentication token to use when sending commands back and forth
5058 // between the websocket instance.
51- setToken ( token : string ) : this {
59+ setToken ( token : string , isUpdate = false ) : this {
5260 this . token = token ;
5361
54- if ( this . url ) {
55- this . send ( 'auth' , token ) ;
62+ if ( isUpdate ) {
63+ this . authenticate ( ) ;
5664 }
5765
5866 return this ;
@@ -63,6 +71,12 @@ export class Websocket extends EventEmitter {
6371 return this . token ;
6472 }
6573
74+ authenticate ( ) {
75+ if ( this . url && this . token ) {
76+ this . send ( 'auth' , this . token ) ;
77+ }
78+ }
79+
6680 close ( code ?: number , reason ?: string ) {
6781 this . url = null ;
6882 this . token = '' ;
0 commit comments