* Emits to this client. * * @example * io.on("connection", (socket) => { * socket.emit("hello", "world"); * * // all serializable datastructures are supported (no need to call JSON.stringify) * socket.emit("hello", 1, "2", { 3: ["4"], 5: Buffer.from([6]) }); * * //
(
ev: Ev,
...args: EventParams<EmitEvents, Ev>
)
| 233 | * @return Always returns `true`. |
| 234 | */ |
| 235 | public emit<Ev extends EventNames<EmitEvents>>( |
| 236 | ev: Ev, |
| 237 | ...args: EventParams<EmitEvents, Ev> |
| 238 | ): boolean { |
| 239 | if (RESERVED_EVENTS.has(ev)) { |
| 240 | throw new Error(`"${String(ev)}" is a reserved event name`); |
| 241 | } |
| 242 | const data: any[] = [ev, ...args]; |
| 243 | const packet: any = { |
| 244 | type: PacketType.EVENT, |
| 245 | data: data, |
| 246 | }; |
| 247 | |
| 248 | // access last argument to see if it's an ACK callback |
| 249 | if (typeof data[data.length - 1] === "function") { |
| 250 | const id = this.nsp._ids++; |
| 251 | debug("emitting packet with ack id %d", id); |
| 252 | |
| 253 | this.registerAckCallback(id, data.pop()); |
| 254 | packet.id = id; |
| 255 | } |
| 256 | |
| 257 | const flags = Object.assign({}, this.flags); |
| 258 | this.flags = {}; |
| 259 | |
| 260 | // @ts-ignore |
| 261 | if (this.nsp.server.opts.connectionStateRecovery) { |
| 262 | // this ensures the packet is stored and can be transmitted upon reconnection |
| 263 | this.adapter.broadcast(packet, { |
| 264 | rooms: new Set([this.id]), |
| 265 | except: new Set(), |
| 266 | flags, |
| 267 | }); |
| 268 | } else { |
| 269 | this.notifyOutgoingListeners(packet); |
| 270 | this.packet(packet, flags); |
| 271 | } |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Emits an event and waits for an acknowledgement |
no test coverage detected