Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /opt/push-server/tests/api/ |
Upload File : |
const { EventEmitter } = require("events"); const Client = require("./client"); const { Request } = require("../../lib/models"); class Chat extends EventEmitter { /** * @param {number} numberOfClients * @param {number} [startClientId=0] */ constructor(numberOfClients, startClientId) { super(); this.clients = []; this.connectedClients = 0; this.maxMessages = 0; this.numberOfMessages = 0; startClientId = startClientId || 0; for (let i = 1; i <= numberOfClients; i++) { const client = new Client(i + startClientId); client.on("message", this.handleClientMessage.bind(this, client)); client.on("connection", this.handleClientConnection.bind(this, client)); client.on("error", this.handleClientError.bind(this)); this.clients.push(client); } } connect() { this.getClients().forEach(client => client.connect()); } disconnect() { this.getClients().forEach(client => client.disconnect()); } setMaxMessages(n) { this.maxMessages = n; } getMaxMessages() { return this.maxMessages; } handleClientMessage(client) { this.numberOfMessages++; if (this.numberOfMessages === this.getMaxMessages()) { this.emit("ready"); } } handleClientConnection(client) { this.connectedClients++; if (this.connectedClients === this.clients.length) { this.emit("connection"); } } handleClientError(error) { this.emit("onerror", error); } /** * * @return {Client[]} */ getClients() { return this.clients; } } module.exports = Chat;