yload.length > MAX_LOG_LENGTH) {
        // Even if we truncate individual values, the resulting message might be
        // huge if we are serializing big objects with many properties or items.
        // Truncate the overall message to avoid issues in logs.
        const truncated = payload.substring(0, MAX_LOG_LENGTH);
        payload = `${truncated} [... truncated after ${MAX_LOG_LENGTH} characters]`;
      }

      lazy.logger.debug(
        `${this.constructor.name} ${this.id} ${direction} ${payload}`
      );
    }
  }

  /**
   * Close the WebSocket connection.
   */
  close() {
    this.transport.close();
  }

  /**
   * Register a new Session to forward the messages to.
   *
   * Needs to be implemented in the sub class.
   */
  registerSession() {
    throw new Error("Not implemented");
  }

  /**
   * Send the JSON-serializable object to the client.
   *
   * @param {object} data
   *     The object to be sent.
   */
  send(data) {
    this.#log("<-", data);
    this.transport.send(data);
  }

  /**
   * Send an error back to the client.
   *
   * Needs to be implemented in the sub class.
   */
  sendError() {
    throw new Error("Not implemented");
  }

  /**
   * Send an event back to the client.
   *
   * Needs to be implemented in the sub class.
   */
  sendEvent() {
    throw new Error("Not implemented");
  }

  /**
   * Send the result of a call to a method back to the client.
   *
   * Needs to be implemented in the sub class.
   */
  sendResult() {
    throw new Error("Not implemented");
  }

  toString() {
    return `[object ${this.constructor.name} ${this.id}]`;
  }

  // Transport hooks

  /**
   * Called by the `transport` when the connection is closed.
   */
  onConnectionClose() {
    lazy.logger.debug(`${this.constructor.name} ${this.id} closed`);
  }

  /**
   * Called when the socket is closed.
   */
  onSocketClose() {
    // In addition to the WebSocket transport, we also have to close the
    // connection used internally within httpd.js. Otherwise the server doesn't
    // shut down correctly, and keeps these Connection instances alive.
    this.httpdConnection.close();
  }

  /**
   * Receive a packet from the WebSocket layer.
   *
   * This packet is sent by a WebSocket client and is meant to execute
   * a particular method.
   *
   * Needs to be implemented in the sub class.
   *
   * @param {object} packet
   *     JSON-serializable object sent by the client.
   */
  async onPacket(packet) {
    this.#log("->", packet);
  }
}
PK