/**
  * Callback when a client sends a message
  *
  * @param \WebSocketServer\Core\Server   $server  The websocket server
  * @param \WebSocketServer\Socket\Client $client  The client
  * @param \WebSocketServer\Socket\Frame  $frame The message
  */
 public function onMessage(Server $server, Client $client, Frame $frame)
 {
     $data = json_decode($frame->getData());
     switch ($data->event) {
         case 'listRooms':
             $client->sendText(json_encode(['event' => $data->event, 'rooms' => $this->getRooms()]));
             break;
         case 'connect':
             $client->sendText(json_encode(['event' => $data->event, 'result' => $this->connect($data->roomId, $data->username), 'roomId' => $data->roomId]));
             break;
         case 'loadRoom':
             $client->sendText(json_encode(['event' => $data->event, 'users' => $this->getUsersOfRoom($data->roomId), 'messages' => []]));
             $room = $this->roomCollection->getById($data->roomId);
             $user = $room->getUser(session_id());
             $server->sendToAllButClient(json_encode(['event' => 'userConnected', 'id' => session_id(), 'username' => $user->getUsername(), 'avatarHash' => md5($user->getUsername()), 'message' => $user->getUsername() . ' entered the room.']), $client);
             break;
         case 'userStartedTyping':
             $server->broadcast(json_encode(['event' => $data->event, 'id' => session_id()]));
             break;
         case 'userStoppedTyping':
             $server->broadcast(json_encode(['event' => $data->event, 'id' => session_id()]));
             break;
         case 'sendMessage':
             $room = $this->roomCollection->getById($data->roomId);
             $user = $room->getUser(session_id());
             $server->broadcast(json_encode(['event' => $data->event, 'message' => ['type' => 'post', 'text' => $data->message, 'user' => ['id' => session_id(), 'username' => $user->getUsername(), 'avatarHash' => md5($user->getUsername())]]]));
             break;
     }
 }
示例#2
0
 /**
  * Remove a client (stream) of the user
  *
  * @param \WebSocketServer\Socket\Client $client The new client to remove of the user
  */
 public function removeClient(Client $client)
 {
     unset($this->clients[$client->getId()]);
 }
 /**
  * Callback when a client suffers an error
  *
  * @param \WebSocketServer\Event\Event   $event   The event
  * @param \WebSocketServer\Socket\Client $client  The client
  * @param string                         $message The error description
  */
 public function onError(Event $event, Client $client, $message)
 {
     if ($user = $client->getAppData('user')) {
         $userId = $user->getId();
     } else {
         $userId = '#' . $client->getId();
     }
     $client->getServer()->sendToAllButClient($userId . ' fell over', $client);
 }
示例#4
0
 /**
  * Disconnect client
  *
  * @param \WebSocketServer\Socket\Client $client The client to disconnect
  *
  * @throws \OutOfRangeException When passed client does not belong to this server
  */
 public function removeClient(Client $client)
 {
     if ($client->getServer() !== $this) {
         throw new \OutOfRangeException('Client does not belong to this server');
     }
     if ($client->isConnected()) {
         $client->disconnect();
     }
     $id = $client->getId();
     if (isset($this->clients[$id])) {
         unset($this->clients[$id], $this->sockets[$id]);
         $this->trigger('clientremove', $this, $client);
     }
 }