Skip to content

Commit 401c384

Browse files
committed
Use EventEmitter API for ws due to websockets/ws#1818
1 parent dbec3d7 commit 401c384

File tree

1 file changed

+84
-8
lines changed

1 file changed

+84
-8
lines changed

client.ts

+84-8
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,87 @@ class ClientEvent extends Event
3434
{
3535
super(error ? 'error' : type)
3636

37+
this.connectionId = connectionId
3738
this.error = error
3839
this.info = info
3940
this.messageData = messageData
4041
this.rawData = rawData
4142
}
4243
}
4344

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+
44118

45119
export class Client extends EventTarget
46120
{
@@ -52,10 +126,10 @@ export class Client extends EventTarget
52126

53127
ws.binaryType = 'arraybuffer'
54128

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)
59133

60134
this.#ws = ws
61135
}
@@ -185,11 +259,13 @@ export class Client extends EventTarget
185259
#version_resolve
186260
#ws
187261

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))
191267

192-
#onMessage = ({data}) =>
268+
#onMessage = data =>
193269
{
194270
const {
195271
ConnectionId, Info, MessageData, RawData, Type

0 commit comments

Comments
 (0)