/** * This method is called when a WebSocket connection is closed from the WebSocket server. * * This method close the connection properly and alert services that a client is disconnected * * @coroutine * * @param Client $client * * @return \Generator|\Icicle\Awaitable\Awaitable|null */ public function onDisconnection(Client $client) { // yield $this->services['chatService']->disconnectUser($client); $this->clients->remove($client); $this->logger->log(LogLevel::INFO, '[WebSocket] :: Client disconnected => ' . $client); }
/** * Room array representation * * @return array The Room array representation */ public function __toArray() : array { return array_merge(parent::__toArray(), ['roomBan' => $this->roomBanCollection->__toArray(), 'clients' => $this->clients->__toArray(), 'pseudonyms' => $this->pseudonyms]); }
/** * Send a message to a client * * @param Client $clientFrom The client to send the message from * @param Client $clientTo The client to send the message to * @param Room $room The room * @param string $message The text message * @param string $type The message type ('public' || 'private') * @param float $date The server timestamp at the moment the message was sent DEFAULT null * @param bool $indexed If the messages is already indexed in ES DEFAULT false * * @return \Generator */ private function sendMessageToClient(Client $clientFrom, Client $clientTo, Room $room, string $message, string $type, float $date = null, bool $indexed = false) { if ($clientFrom->getConnection()->getRemoteAddress() === '127.0.0.1') { $pseudonym = 'SERVER'; } else { $pseudonym = $room->getClientPseudonym($clientFrom); } $date = $date !== null ? $date : static::microtimeAsInt(); (yield $clientTo->getConnection()->send(json_encode(['service' => $this->serviceName, 'action' => 'receiveMessage', 'pseudonym' => $pseudonym, 'date' => $date, 'roomId' => $room->id, 'type' => $type, 'message' => $message]))); if (!$indexed) { // Insert elasticSearch record $clientsTo = new ClientCollection(); $clientsTo->add($clientTo); $this->indexMessage($clientFrom, $clientsTo, $room, $message, $type, $date); } }