feat: 同步 socket.io 上游代码

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2023-02-09 13:49:48 +08:00
parent 359aeb9d63
commit 7b85ff5b7c
35 changed files with 7612 additions and 6519 deletions

View File

@@ -1,111 +1,111 @@
import { Transport } from "../transport"
import { Transport } from "../transport";
// import debugModule from "debug";
const debug = require('../../debug')("engine:ws")
const debug = require("../../debug")("engine:ws");
export class WebSocket extends Transport {
protected perMessageDeflate: any
private socket: any
protected perMessageDeflate: any;
private socket: any;
/**
* WebSocket transport
*
* @param {http.IncomingMessage}
* @api public
*/
constructor(req) {
super(req)
this.socket = req.websocket
this.socket.on("message", (data, isBinary) => {
const message = isBinary ? data : data.toString()
debug('received "%s"', message)
super.onData(message)
})
this.socket.once("close", this.onClose.bind(this))
this.socket.on("error", this.onError.bind(this))
this.writable = true
this.perMessageDeflate = null
/**
* WebSocket transport
*
* @param {http.IncomingMessage}
* @api public
*/
constructor(req) {
super(req);
this.socket = req.websocket;
this.socket.on("message", (data, isBinary) => {
const message = isBinary ? data : data.toString();
debug('received "%s"', message);
super.onData(message);
});
this.socket.once("close", this.onClose.bind(this));
this.socket.on("error", this.onError.bind(this));
this.writable = true;
this.perMessageDeflate = null;
}
/**
* Transport name
*
* @api public
*/
get name() {
return "websocket";
}
/**
* Advertise upgrade support.
*
* @api public
*/
get handlesUpgrades() {
return true;
}
/**
* Advertise framing support.
*
* @api public
*/
get supportsFraming() {
return true;
}
/**
* Writes a packet payload.
*
* @param {Array} packets
* @api private
*/
send(packets) {
const packet = packets.shift();
if (typeof packet === "undefined") {
this.writable = true;
this.emit("drain");
return;
}
/**
* Transport name
*
* @api public
*/
get name() {
return "websocket"
// always creates a new object since ws modifies it
const opts: { compress?: boolean } = {};
if (packet.options) {
opts.compress = packet.options.compress;
}
/**
* Advertise upgrade support.
*
* @api public
*/
get handlesUpgrades() {
return true
}
/**
* Advertise framing support.
*
* @api public
*/
get supportsFraming() {
return true
}
/**
* Writes a packet payload.
*
* @param {Array} packets
* @api private
*/
send(packets) {
const packet = packets.shift()
if (typeof packet === "undefined") {
this.writable = true
this.emit("drain")
return
const send = data => {
if (this.perMessageDeflate) {
const len =
"string" === typeof data ? Buffer.byteLength(data) : data.length;
if (len < this.perMessageDeflate.threshold) {
opts.compress = false;
}
}
debug('writing "%s"', data);
this.writable = false;
// always creates a new object since ws modifies it
const opts: { compress?: boolean } = {}
if (packet.options) {
opts.compress = packet.options.compress
}
this.socket.send(data, opts, err => {
if (err) return this.onError("write error", err.stack);
this.send(packets);
});
};
const send = data => {
if (this.perMessageDeflate) {
const len =
"string" === typeof data ? Buffer.byteLength(data) : data.length
if (len < this.perMessageDeflate.threshold) {
opts.compress = false
}
}
debug('writing "%s"', data)
this.writable = false
this.socket.send(data, opts, err => {
if (err) return this.onError("write error", err.stack)
this.send(packets)
})
}
if (packet.options && typeof packet.options.wsPreEncoded === "string") {
send(packet.options.wsPreEncoded)
} else {
this.parser.encodePacket(packet, this.supportsBinary, send)
}
if (packet.options && typeof packet.options.wsPreEncoded === "string") {
send(packet.options.wsPreEncoded);
} else {
this.parser.encodePacket(packet, this.supportsBinary, send);
}
}
/**
* Closes the transport.
*
* @api private
*/
doClose(fn) {
debug("closing")
this.socket.close()
fn && fn()
}
/**
* Closes the transport.
*
* @api private
*/
doClose(fn) {
debug("closing");
this.socket.close();
fn && fn();
}
}