Files
socket.io/lib/transport.js
Simone Mazzoni 868d89111d fix: set default protocol version to 3 (#616)
socket.io-client-swift libs version <=15.2.0, which uses protocol
version 3, do not explicitly add the EIO query parameter at transport
initialization. This omission leads the server to treat the client as
a client that supports the protocol version 4, previously set as
default, which is not correct for those versions of the client lib.

From socket.io-client-swift version v16.0.0 the EIO query parameter is
explicitly passed to specify the protocol version supported, but since
the allowEIO3 parameter aims to make the server compatible with
previous versions which in most of the cases are already used in
production and not easily upgradable, it makes more sense to default
the EIO version to 3 if not explicitly set by the client since the
newer client versions pass the EIO protocol version in query
parameters.

Related: https://github.com/socketio/socket.io/issues/3794
2021-03-09 08:23:36 +01:00

111 lines
2.1 KiB
JavaScript

const EventEmitter = require("events");
const parser_v4 = require("engine.io-parser");
const parser_v3 = require("./parser-v3/index");
const debug = require("debug")("engine:transport");
/**
* Noop function.
*
* @api private
*/
function noop() {}
class Transport extends EventEmitter {
/**
* Transport constructor.
*
* @param {http.IncomingMessage} request
* @api public
*/
constructor(req) {
super();
this.readyState = "open";
this.discarded = false;
this.protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
this.parser = this.protocol === 4 ? parser_v4 : parser_v3;
}
/**
* Flags the transport as discarded.
*
* @api private
*/
discard() {
this.discarded = true;
}
/**
* Called with an incoming HTTP request.
*
* @param {http.IncomingMessage} request
* @api private
*/
onRequest(req) {
debug("setting request");
this.req = req;
}
/**
* Closes the transport.
*
* @api private
*/
close(fn) {
if ("closed" === this.readyState || "closing" === this.readyState) return;
this.readyState = "closing";
this.doClose(fn || noop);
}
/**
* Called with a transport error.
*
* @param {String} message error
* @param {Object} error description
* @api private
*/
onError(msg, desc) {
if (this.listeners("error").length) {
const err = new Error(msg);
err.type = "TransportError";
err.description = desc;
this.emit("error", err);
} else {
debug("ignored transport error %s (%s)", msg, desc);
}
}
/**
* Called with parsed out a packets from the data stream.
*
* @param {Object} packet
* @api private
*/
onPacket(packet) {
this.emit("packet", packet);
}
/**
* Called with the encoded packet data.
*
* @param {String} data
* @api private
*/
onData(data) {
this.onPacket(this.parser.decodePacket(data));
}
/**
* Called upon transport close.
*
* @api private
*/
onClose() {
this.readyState = "closed";
this.emit("close");
}
}
module.exports = Transport;