Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.log(1, "connect: begin")
/* create a new WebSocket client */
let ws
if (process.env.PLATFORM === "browser")
ws = new WebSocket(this._args.uri, this._args.opts.protocols)
else {
const opts = this.hook("connect:options", "pass", {})
ws = new WebSocket(this._args.uri, this._args.opts.protocols, opts)
}
/* configure binary transfer */
ws.binaryType = process.env.PLATFORM === "browser" ? "arraybuffer" : "nodebuffer"
/* create a new WebSocket-Framed wrapper */
const wsf = new WebSocketFramed(ws, this._args.opts.encoding)
/* react (once) on error messages */
const onError = (ev) => {
if (this._ws !== null && this._ws._explicitDisconnect)
return
this.log(1, `connect: end (connection error: ${ev.message})`)
ws.removeEventListener("error", onError)
ws._errorOnConnect = true
if (attempt < this._args.opts.reconnectattempts || this._args.opts.reconnectattempts === -1) {
this.log(2, "connection error: trigger new connect attempt " +
`(in ${Math.trunc(this._args.opts.reconnectdelay / 1000)}s)`)
setTimeout(() => {
/* handle repeated connection attempts (subsequently) */
connectInternal(attempt + 1)
.then(() => resolve())
.catch((err) => reject(err))