divvy-lib
Version:
A JavaScript API for interacting with Divvy in Node.js and the browser
42 lines • 1.09 kB
JavaScript
"use strict";
const events_1 = require("events");
/**
* Provides `EventEmitter` interface for native browser `WebSocket`,
* same, as `ws` package provides.
*/
class WSWrapper extends events_1.EventEmitter {
constructor(url, _protocols, _websocketOptions) {
super();
this.setMaxListeners(Infinity);
this._ws = new WebSocket(url);
this._ws.onclose = () => {
this.emit('close');
};
this._ws.onopen = () => {
this.emit('open');
};
this._ws.onerror = error => {
this.emit('error', error);
};
this._ws.onmessage = message => {
this.emit('message', message.data);
};
}
close() {
if (this.readyState === 1) {
this._ws.close();
}
}
send(message) {
this._ws.send(message);
}
get readyState() {
return this._ws.readyState;
}
}
WSWrapper.CONNECTING = 0;
WSWrapper.OPEN = 1;
WSWrapper.CLOSING = 2;
WSWrapper.CLOSED = 3;
module.exports = WSWrapper;
//# sourceMappingURL=wswrapper.js.map