123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { decodePacket } from "engine.io-parser";
- import { Emitter } from "@socket.io/component-emitter";
- import { installTimerFunctions } from "./util.js";
- import debugModule from "debug"; // debug()
- const debug = debugModule("engine.io-client:transport"); // debug()
- export class Transport extends Emitter {
- /**
- * Transport abstract constructor.
- *
- * @param {Object} options.
- * @api private
- */
- constructor(opts) {
- super();
- this.writable = false;
- installTimerFunctions(this, opts);
- this.opts = opts;
- this.query = opts.query;
- this.readyState = "";
- this.socket = opts.socket;
- }
- /**
- * Emits an error.
- *
- * @param {String} str
- * @return {Transport} for chaining
- * @api protected
- */
- onError(msg, desc) {
- const err = new Error(msg);
- // @ts-ignore
- err.type = "TransportError";
- // @ts-ignore
- err.description = desc;
- super.emit("error", err);
- return this;
- }
- /**
- * Opens the transport.
- *
- * @api public
- */
- open() {
- if ("closed" === this.readyState || "" === this.readyState) {
- this.readyState = "opening";
- this.doOpen();
- }
- return this;
- }
- /**
- * Closes the transport.
- *
- * @api public
- */
- close() {
- if ("opening" === this.readyState || "open" === this.readyState) {
- this.doClose();
- this.onClose();
- }
- return this;
- }
- /**
- * Sends multiple packets.
- *
- * @param {Array} packets
- * @api public
- */
- send(packets) {
- if ("open" === this.readyState) {
- this.write(packets);
- }
- else {
- // this might happen if the transport was silently closed in the beforeunload event handler
- debug("transport is not open, discarding packets");
- }
- }
- /**
- * Called upon open
- *
- * @api protected
- */
- onOpen() {
- this.readyState = "open";
- this.writable = true;
- super.emit("open");
- }
- /**
- * Called with data.
- *
- * @param {String} data
- * @api protected
- */
- onData(data) {
- const packet = decodePacket(data, this.socket.binaryType);
- this.onPacket(packet);
- }
- /**
- * Called with a decoded packet.
- *
- * @api protected
- */
- onPacket(packet) {
- super.emit("packet", packet);
- }
- /**
- * Called upon close.
- *
- * @api protected
- */
- onClose() {
- this.readyState = "closed";
- super.emit("close");
- }
- }
|