120 lines
2.5 KiB
TypeScript
120 lines
2.5 KiB
TypeScript
import parser from "../engine.io-parser"
|
|
const Emitter = require("component-emitter")
|
|
import { installTimerFunctions } from "./util"
|
|
const debug = (...args: any) => console.debug('engine.io-client:transport', ...args)//require("debug")("engine.io-client:transport")
|
|
|
|
export class Transport extends Emitter {
|
|
/**
|
|
* Transport abstract constructor.
|
|
*
|
|
* @param {Object} options.
|
|
* @api private
|
|
*/
|
|
constructor(opts) {
|
|
super()
|
|
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 public
|
|
*/
|
|
onError(msg, desc) {
|
|
const err: any = 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 {
|
|
// 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 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")
|
|
}
|
|
}
|