Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / devtools / shared / transport / websocket-transport.js
blobb8255e0067b057d388cf95b08f51f76f0c136929
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 "use strict";
7 const EventEmitter = require("resource://devtools/shared/event-emitter.js");
9 function WebSocketDebuggerTransport(socket) {
10 EventEmitter.decorate(this);
12 this.active = false;
13 this.hooks = null;
14 this.socket = socket;
17 WebSocketDebuggerTransport.prototype = {
18 ready() {
19 if (this.active) {
20 return;
23 this.socket.addEventListener("message", this);
24 this.socket.addEventListener("close", this);
26 this.active = true;
29 send(object) {
30 this.emit("send", object);
31 if (this.socket) {
32 this.socket.send(JSON.stringify(object));
36 startBulkSend() {
37 throw new Error("Bulk send is not supported by WebSocket transport");
40 close() {
41 if (!this.socket) {
42 return;
44 this.emit("close");
45 this.active = false;
47 this.socket.removeEventListener("message", this);
48 this.socket.removeEventListener("close", this);
49 this.socket.close();
50 this.socket = null;
52 if (this.hooks) {
53 if (this.hooks.onTransportClosed) {
54 this.hooks.onTransportClosed();
56 this.hooks = null;
60 handleEvent(event) {
61 switch (event.type) {
62 case "message":
63 this.onMessage(event);
64 break;
65 case "close":
66 this.close();
67 break;
71 onMessage({ data }) {
72 if (typeof data !== "string") {
73 throw new Error(
74 "Binary messages are not supported by WebSocket transport"
78 const object = JSON.parse(data);
79 this.emit("packet", object);
80 if (this.hooks) {
81 this.hooks.onPacket(object);
86 module.exports = WebSocketDebuggerTransport;