1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // This is an XPCOM service-ified copy of ../devtools/shared/transport/websocket-transport.js.
9 ChromeUtils.defineESModuleGetters(lazy, {
10 EventEmitter: "resource://gre/modules/EventEmitter.sys.mjs",
13 export function WebSocketTransport(socket) {
14 lazy.EventEmitter.decorate(this);
21 WebSocketTransport.prototype = {
27 this.socket.addEventListener("message", this);
28 this.socket.addEventListener("close", this);
34 this.emit("send", object);
36 this.socket.send(JSON.stringify(object));
41 throw new Error("Bulk send is not supported by WebSocket transport");
51 this.socket.removeEventListener("message", this);
52 this.socket.removeEventListener("close", this);
57 this.hooks.onClosed();
65 this.onMessage(event);
74 if (typeof data !== "string") {
76 "Binary messages are not supported by WebSocket transport"
80 const object = JSON.parse(data);
81 this.emit("packet", object);
83 this.hooks.onPacket(object);