Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/cri/deno-cri/websocket-wrapper.js
3587 views
1
/*
2
* websocket-wrapper.js
3
*
4
* Copyright (c) 2021 Andrea Cardaci <[email protected]>
5
*
6
* Deno port Copyright (C) 2022 Posit Software, PBC
7
*/
8
9
import EventEmitter from "events/mod.ts";
10
11
// wrapper around the Node.js ws module
12
// for use in browsers
13
export class WebSocketWrapper extends EventEmitter {
14
constructor(url) {
15
super();
16
this._ws = new WebSocket(url); // eslint-disable-line no-undef
17
this._ws.onopen = () => {
18
this.emit("open");
19
};
20
this._ws.onclose = () => {
21
this.emit("close");
22
};
23
this._ws.onmessage = (event) => {
24
this.emit("message", event.data);
25
};
26
this._ws.onerror = () => {
27
this.emit("error", new Error("WebSocket error"));
28
};
29
}
30
31
close() {
32
this._ws.close();
33
}
34
35
send(data, callback) {
36
try {
37
this._ws.send(data);
38
callback();
39
} catch (err) {
40
callback(err);
41
}
42
}
43
}
44
45