@@ -4,20 +4,19 @@ import { SocketConfig } from "./interfaces/BITBOXInterfaces"
44const io : any = require ( "socket.io-client" )
55export class Socket {
66 socket : any
7+ websocketURL : string
78 bitsocketURL : string
89 constructor ( config : SocketConfig = { } ) {
9- let websocketURL : string = ""
1010 if ( config . wsURL ) {
1111 // default to passed in wsURL
12- websocketURL = config . wsURL
12+ this . websocketURL = config . wsURL
1313 } else if ( config . restURL ) {
1414 // 2nd option deprecated restURL
15- websocketURL = config . restURL
15+ this . websocketURL = config . restURL
1616 } else {
1717 // fallback to WS_URL
18- websocketURL = WS_URL
18+ this . websocketURL = WS_URL
1919 }
20- this . socket = io ( websocketURL , { transports : [ "websocket" ] } )
2120
2221 if ( config . bitsocketURL ) {
2322 this . bitsocketURL = config . bitsocketURL
@@ -30,6 +29,7 @@ export class Socket {
3029
3130 public listen ( query : string , cb : Function ) : void {
3231 if ( query === "blocks" || query === "transactions" ) {
32+ this . socket = io ( this . websocketURL , { transports : [ "websocket" ] } )
3333 this . socket . emit ( query )
3434
3535 if ( query === "blocks" ) this . socket . on ( "blocks" , ( msg : any ) => cb ( msg ) )
@@ -38,9 +38,8 @@ export class Socket {
3838 } else {
3939 let EventSource = require ( "eventsource" )
4040 let b64 = Buffer . from ( JSON . stringify ( query ) ) . toString ( "base64" )
41- let socket = new EventSource ( `${ this . bitsocketURL } /s/${ b64 } ` )
42-
43- socket . onmessage = ( msg : any ) => {
41+ this . socket = new EventSource ( `${ this . bitsocketURL } /s/${ b64 } ` )
42+ this . socket . onmessage = ( msg : any ) => {
4443 cb ( msg . data )
4544 }
4645 }
0 commit comments