/** * Create a client array of a Connection and a User object and handle the client session with until the connection * is closed * * @coroutine * * @param \Icicle\WebSocket\Connection $connection * * @return \Generator|\Icicle\Awaitable\Awaitable|null */ private function connectionSessionHandle(Connection $connection) { $client = new Client($connection); $iterator = $connection->read()->getIterator(); $this->clients->add($client); (yield $connection->send(json_encode(['service' => 'clientService', 'action' => 'connect', 'id' => $client->getId(), 'connection' => ['remoteAddress' => $connection->getRemoteAddress(), 'remotePort' => $connection->getRemotePort()]]))); while ((yield $iterator->isValid())) { (yield $this->serviceSelector(json_decode($iterator->getCurrent()->getData(), true), $client)); $this->logger->log(LogLevel::DEBUG, $this->rooms[0]); } $this->onDisconnection($client); }
/** * 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); } }