Path: blob/main/src/core/cri/deno-cri/websocket-wrapper.js
3587 views
/*1* websocket-wrapper.js2*3* Copyright (c) 2021 Andrea Cardaci <[email protected]>4*5* Deno port Copyright (C) 2022 Posit Software, PBC6*/78import EventEmitter from "events/mod.ts";910// wrapper around the Node.js ws module11// for use in browsers12export class WebSocketWrapper extends EventEmitter {13constructor(url) {14super();15this._ws = new WebSocket(url); // eslint-disable-line no-undef16this._ws.onopen = () => {17this.emit("open");18};19this._ws.onclose = () => {20this.emit("close");21};22this._ws.onmessage = (event) => {23this.emit("message", event.data);24};25this._ws.onerror = () => {26this.emit("error", new Error("WebSocket error"));27};28}2930close() {31this._ws.close();32}3334send(data, callback) {35try {36this._ws.send(data);37callback();38} catch (err) {39callback(err);40}41}42}434445