* Override `emit`. * If the event is in `events`, it's emitted normally. * * @example * socket.emit("hello", "world"); * * // all serializable datastructures are supported (no need to call JSON.stringify) * socket.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
(
ev: Ev,
...args: EventParams<EmitEvents, Ev>
)
| 408 | * @return self |
| 409 | */ |
| 410 | public emit<Ev extends EventNames<EmitEvents>>( |
| 411 | ev: Ev, |
| 412 | ...args: EventParams<EmitEvents, Ev> |
| 413 | ): this { |
| 414 | if (RESERVED_EVENTS.hasOwnProperty(ev)) { |
| 415 | throw new Error('"' + ev.toString() + '" is a reserved event name'); |
| 416 | } |
| 417 | |
| 418 | args.unshift(ev); |
| 419 | |
| 420 | if (this._opts.retries && !this.flags.fromQueue && !this.flags.volatile) { |
| 421 | this._addToQueue(args); |
| 422 | return this; |
| 423 | } |
| 424 | |
| 425 | const packet: any = { |
| 426 | type: PacketType.EVENT, |
| 427 | data: args, |
| 428 | }; |
| 429 | |
| 430 | packet.options = {}; |
| 431 | packet.options.compress = this.flags.compress !== false; |
| 432 | |
| 433 | // event ack callback |
| 434 | if ("function" === typeof args[args.length - 1]) { |
| 435 | const id = this.ids++; |
| 436 | debug("emitting packet with ack id %d", id); |
| 437 | |
| 438 | const ack = args.pop() as (...args: any[]) => void; |
| 439 | this._registerAckCallback(id, ack); |
| 440 | packet.id = id; |
| 441 | } |
| 442 | |
| 443 | const isTransportWritable = this.io.engine?.transport?.writable; |
| 444 | const isConnected = this.connected && !this.io.engine?._hasPingExpired(); |
| 445 | |
| 446 | const discardPacket = this.flags.volatile && !isTransportWritable; |
| 447 | if (discardPacket) { |
| 448 | debug("discard packet as the transport is not currently writable"); |
| 449 | } else if (isConnected) { |
| 450 | this.notifyOutgoingListeners(packet); |
| 451 | this.packet(packet); |
| 452 | } else { |
| 453 | this.sendBuffer.push(packet); |
| 454 | } |
| 455 | |
| 456 | this.flags = {}; |
| 457 | |
| 458 | return this; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @private |
no test coverage detected