@@ -34,13 +34,87 @@ class ClientEvent extends Event
34
34
{
35
35
super ( error ? 'error' : type )
36
36
37
+ this . connectionId = connectionId
37
38
this . error = error
38
39
this . info = info
39
40
this . messageData = messageData
40
41
this . rawData = rawData
41
42
}
42
43
}
43
44
45
+ /**
46
+ * Class representing a close event.
47
+ *
48
+ * @extends Event
49
+ * @private
50
+ */
51
+ class CloseEvent extends Event {
52
+ code
53
+ reason
54
+ wasClean
55
+
56
+ /**
57
+ * Create a new `CloseEvent`.
58
+ *
59
+ * @param {Number } code The status code explaining why the connection is being
60
+ * closed
61
+ * @param {String } reason A human-readable string explaining why the
62
+ * connection is closing
63
+ * @param {WebSocket } target A reference to the target to which the event was
64
+ * dispatched
65
+ */
66
+ constructor ( code , reason , target ) {
67
+ super ( 'close' ) ;
68
+
69
+ this . wasClean = target . _closeFrameReceived && target . _closeFrameSent ;
70
+ this . reason = reason ;
71
+ this . code = code ;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Class representing an open event.
77
+ *
78
+ * @extends Event
79
+ * @private
80
+ */
81
+ class OpenEvent extends Event {
82
+ /**
83
+ * Create a new `OpenEvent`.
84
+ *
85
+ * @param {WebSocket } target A reference to the target to which the event was
86
+ * dispatched
87
+ */
88
+ constructor ( target ) {
89
+ super ( 'open' ) ;
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Class representing an error event.
95
+ *
96
+ * @extends Event
97
+ * @private
98
+ */
99
+ class ErrorEvent extends Event {
100
+ error
101
+ message
102
+
103
+ /**
104
+ * Create a new `ErrorEvent`.
105
+ *
106
+ * @param {Object } error The error that generated this event
107
+ * @param {WebSocket } target A reference to the target to which the event was
108
+ * dispatched
109
+ */
110
+ constructor ( error , target ) {
111
+ super ( 'error' ) ;
112
+
113
+ this . message = error . message ;
114
+ this . error = error ;
115
+ }
116
+ }
117
+
44
118
45
119
export class Client extends EventTarget
46
120
{
@@ -52,10 +126,10 @@ export class Client extends EventTarget
52
126
53
127
ws . binaryType = 'arraybuffer'
54
128
55
- ws . addEventListener ( 'close' , this . #onClose, { once : true } )
56
- ws . addEventListener ( 'error' , this . #onError)
57
- ws . addEventListener ( 'message' , this . #onMessage)
58
- ws . addEventListener ( 'open' , this . #onOpen, { once : true } )
129
+ ws . once ( 'close' , this . #onClose)
130
+ ws . on ( 'error' , this . #onError)
131
+ ws . on ( 'message' , this . #onMessage)
132
+ ws . once ( 'open' , this . #onOpen)
59
133
60
134
this . #ws = ws
61
135
}
@@ -185,11 +259,13 @@ export class Client extends EventTarget
185
259
#version_resolve
186
260
#ws
187
261
188
- #onClose = this . dispatchEvent . bind ( this )
189
- #onError = this . dispatchEvent . bind ( this )
190
- #onOpen = this . dispatchEvent . bind ( this )
262
+ #onClose = ( code , reason ) =>
263
+ this . dispatchEvent ( new CloseEvent ( code , reason , this . #ws) )
264
+ #onError = error =>
265
+ this . dispatchEvent ( new ErrorEvent ( error , this . #ws) )
266
+ #onOpen = ( ) => this . dispatchEvent ( new OpenEvent ( this . #ws) )
191
267
192
- #onMessage = ( { data} ) =>
268
+ #onMessage = data =>
193
269
{
194
270
const {
195
271
ConnectionId, Info, MessageData, RawData, Type
0 commit comments