@@ -43,14 +43,13 @@ export class BaseSocket extends EventSpewer {
43
43
constructor ( url : string ) {
44
44
super ( ) ;
45
45
this . socket = new WebsocketDependency . module ( url ) ;
46
+ this . socket . on ( SocketEventsBase . CLOSE , this . onClose . bind ( this ) ) ;
46
47
this . socket . on ( SocketEventsBase . PONG , this . onPong . bind ( this ) ) ;
47
48
48
- for ( let event of Object . values ( SocketEventsBase ) ) {
49
- if ( event === SocketEventsBase . PONG ) {
50
- continue ;
51
- }
52
- this . socket . on ( event , this . emit . bind ( this , event ) ) ;
53
- }
49
+ this . socket . on ( SocketEventsBase . ERROR , this . emit . bind ( this , SocketEventsBase . ERROR ) ) ;
50
+ this . socket . on ( SocketEventsBase . MESSAGE , this . emit . bind ( this , SocketEventsBase . MESSAGE ) ) ;
51
+ this . socket . on ( SocketEventsBase . OPEN , this . emit . bind ( this , SocketEventsBase . OPEN ) ) ;
52
+ this . socket . on ( SocketEventsBase . PING , this . emit . bind ( this , SocketEventsBase . PING ) ) ;
54
53
}
55
54
56
55
get closed ( ) : boolean {
@@ -73,41 +72,34 @@ export class BaseSocket extends EventSpewer {
73
72
return WebsocketDependency . type ;
74
73
}
75
74
76
- send (
77
- data : any ,
78
- callback ?: Function ,
79
- ) : void {
75
+ send ( data : any , callback ?: Function ) : void {
80
76
if ( this . connected ) {
81
77
this . socket . send ( data , { } , callback ) ;
82
78
}
83
79
}
84
80
85
- close (
86
- code : number = SocketCloseCodes . NORMAL ,
87
- reason : string = '' ,
88
- ) : void {
81
+ close ( code : number = SocketCloseCodes . NORMAL , reason : string = '' ) : void {
89
82
if ( this . connected ) {
90
83
this . socket . close ( code , reason ) ;
91
84
}
85
+ }
86
+
87
+ onClose ( code : number , message : string ) : void {
92
88
for ( const [ nonce , { reject} ] of this . pings ) {
93
89
reject ( new Error ( 'Socket has closed.' ) ) ;
94
90
this . pings . delete ( nonce ) ;
95
91
}
96
92
this . pings . clear ( ) ;
97
93
98
- for ( let event of Object . values ( SocketEventsBase ) ) {
99
- // clear out all listeners but close from the socket
100
- if ( event === SocketEventsBase . CLOSE ) {
101
- continue ;
102
- }
103
- this . socket . on ( event , this . emit . bind ( this , event ) ) ;
104
- }
94
+ this . socket . removeAllListeners ( ) ;
95
+ this . emit ( SocketEventsBase . CLOSE , code , message ) ;
96
+
105
97
this . removeAllListeners ( ) ;
106
98
}
107
99
108
100
onPong ( data : any ) : void {
109
101
try {
110
- const { nonce } = JSON . parse ( String ( data ) ) ;
102
+ const { nonce } : { nonce : string } = JSON . parse ( String ( data ) ) ;
111
103
const ping = this . pings . get ( nonce ) ;
112
104
if ( ping ) {
113
105
ping . resolve ( ) ;
@@ -137,10 +129,7 @@ export class BaseSocket extends EventSpewer {
137
129
138
130
const now = Date . now ( ) ;
139
131
new Promise ( ( res , rej ) => {
140
- this . pings . set ( nonce , {
141
- resolve : res ,
142
- reject : rej ,
143
- } ) ;
132
+ this . pings . set ( nonce , { resolve : res , reject : rej } ) ;
144
133
this . socket . ping ( JSON . stringify ( { nonce} ) ) ;
145
134
} ) . then ( ( ) => {
146
135
expire . stop ( ) ;
0 commit comments