|
"use strict"; |
|
var __importDefault = (this && this.__importDefault) || function (mod) { |
|
return (mod && mod.__esModule) ? mod : { "default": mod }; |
|
}; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.WS = void 0; |
|
const transport_js_1 = require("../transport.js"); |
|
const yeast_js_1 = require("../contrib/yeast.js"); |
|
const util_js_1 = require("../util.js"); |
|
const websocket_constructor_js_1 = require("./websocket-constructor.js"); |
|
const debug_1 = __importDefault(require("debug")); |
|
const engine_io_parser_1 = require("engine.io-parser"); |
|
const debug = (0, debug_1.default)("engine.io-client:websocket"); |
|
|
|
const isReactNative = typeof navigator !== "undefined" && |
|
typeof navigator.product === "string" && |
|
navigator.product.toLowerCase() === "reactnative"; |
|
class WS extends transport_js_1.Transport { |
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(opts) { |
|
super(opts); |
|
this.supportsBinary = !opts.forceBase64; |
|
} |
|
get name() { |
|
return "websocket"; |
|
} |
|
doOpen() { |
|
if (!this.check()) { |
|
|
|
return; |
|
} |
|
const uri = this.uri(); |
|
const protocols = this.opts.protocols; |
|
|
|
const opts = isReactNative |
|
? {} |
|
: (0, util_js_1.pick)(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity"); |
|
if (this.opts.extraHeaders) { |
|
opts.headers = this.opts.extraHeaders; |
|
} |
|
try { |
|
this.ws = |
|
websocket_constructor_js_1.usingBrowserWebSocket && !isReactNative |
|
? protocols |
|
? new websocket_constructor_js_1.WebSocket(uri, protocols) |
|
: new websocket_constructor_js_1.WebSocket(uri) |
|
: new websocket_constructor_js_1.WebSocket(uri, protocols, opts); |
|
} |
|
catch (err) { |
|
return this.emitReserved("error", err); |
|
} |
|
this.ws.binaryType = this.socket.binaryType; |
|
this.addEventListeners(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
addEventListeners() { |
|
this.ws.onopen = () => { |
|
if (this.opts.autoUnref) { |
|
this.ws._socket.unref(); |
|
} |
|
this.onOpen(); |
|
}; |
|
this.ws.onclose = (closeEvent) => this.onClose({ |
|
description: "websocket connection closed", |
|
context: closeEvent, |
|
}); |
|
this.ws.onmessage = (ev) => this.onData(ev.data); |
|
this.ws.onerror = (e) => this.onError("websocket error", e); |
|
} |
|
write(packets) { |
|
this.writable = false; |
|
|
|
|
|
for (let i = 0; i < packets.length; i++) { |
|
const packet = packets[i]; |
|
const lastPacket = i === packets.length - 1; |
|
(0, engine_io_parser_1.encodePacket)(packet, this.supportsBinary, (data) => { |
|
|
|
const opts = {}; |
|
if (!websocket_constructor_js_1.usingBrowserWebSocket) { |
|
if (packet.options) { |
|
opts.compress = packet.options.compress; |
|
} |
|
if (this.opts.perMessageDeflate) { |
|
const len = |
|
|
|
"string" === typeof data ? Buffer.byteLength(data) : data.length; |
|
if (len < this.opts.perMessageDeflate.threshold) { |
|
opts.compress = false; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
try { |
|
if (websocket_constructor_js_1.usingBrowserWebSocket) { |
|
|
|
this.ws.send(data); |
|
} |
|
else { |
|
this.ws.send(data, opts); |
|
} |
|
} |
|
catch (e) { |
|
debug("websocket closed before onclose event"); |
|
} |
|
if (lastPacket) { |
|
|
|
|
|
(0, websocket_constructor_js_1.nextTick)(() => { |
|
this.writable = true; |
|
this.emitReserved("drain"); |
|
}, this.setTimeoutFn); |
|
} |
|
}); |
|
} |
|
} |
|
doClose() { |
|
if (typeof this.ws !== "undefined") { |
|
this.ws.close(); |
|
this.ws = null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
uri() { |
|
const schema = this.opts.secure ? "wss" : "ws"; |
|
const query = this.query || {}; |
|
|
|
if (this.opts.timestampRequests) { |
|
query[this.opts.timestampParam] = (0, yeast_js_1.yeast)(); |
|
} |
|
|
|
if (!this.supportsBinary) { |
|
query.b64 = 1; |
|
} |
|
return this.createUri(schema, query); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
check() { |
|
return !!websocket_constructor_js_1.WebSocket; |
|
} |
|
} |
|
exports.WS = WS; |
|
|