You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
1.9 KiB
117 lines
1.9 KiB
const parser = require("engine.io-parser");
|
|
const Emitter = require("component-emitter");
|
|
|
|
class Transport extends Emitter {
|
|
/**
|
|
* Transport abstract constructor.
|
|
*
|
|
* @param {Object} options.
|
|
* @api private
|
|
*/
|
|
constructor(opts) {
|
|
super();
|
|
|
|
this.opts = opts;
|
|
this.query = opts.query;
|
|
this.readyState = "";
|
|
this.socket = opts.socket;
|
|
}
|
|
|
|
/**
|
|
* Emits an error.
|
|
*
|
|
* @param {String} str
|
|
* @return {Transport} for chaining
|
|
* @api public
|
|
*/
|
|
onError(msg, desc) {
|
|
const err = new Error(msg);
|
|
err.type = "TransportError";
|
|
err.description = desc;
|
|
this.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 private
|
|
*/
|
|
close() {
|
|
if ("opening" === this.readyState || "open" === this.readyState) {
|
|
this.doClose();
|
|
this.onClose();
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Sends multiple packets.
|
|
*
|
|
* @param {Array} packets
|
|
* @api private
|
|
*/
|
|
send(packets) {
|
|
if ("open" === this.readyState) {
|
|
this.write(packets);
|
|
} else {
|
|
throw new Error("Transport not open");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Called upon open
|
|
*
|
|
* @api private
|
|
*/
|
|
onOpen() {
|
|
this.readyState = "open";
|
|
this.writable = true;
|
|
this.emit("open");
|
|
}
|
|
|
|
/**
|
|
* Called with data.
|
|
*
|
|
* @param {String} data
|
|
* @api private
|
|
*/
|
|
onData(data) {
|
|
const packet = parser.decodePacket(data, this.socket.binaryType);
|
|
this.onPacket(packet);
|
|
}
|
|
|
|
/**
|
|
* Called with a decoded packet.
|
|
*/
|
|
onPacket(packet) {
|
|
this.emit("packet", packet);
|
|
}
|
|
|
|
/**
|
|
* Called upon close.
|
|
*
|
|
* @api private
|
|
*/
|
|
onClose() {
|
|
this.readyState = "closed";
|
|
this.emit("close");
|
|
}
|
|
}
|
|
|
|
module.exports = Transport;
|
|
|