Exemplo n.º 1
0
 /**
  * Função executada quando o servidor recebe uma mensagem
  *
  * @param ConnectionInterface $connection
  * @param String $jsonMessage
  */
 public function onMessage(ConnectionInterface $connection, $jsonMessage)
 {
     $cache = \Cache::get($connection->session);
     //Envia a mensagem para o MessageManager, ele identificará qual o tipo
     //dela
     $message = MessageManager::createMessage($jsonMessage, $connection);
     if ($message instanceof SlideMessage) {
         Controller::$lastMessage = $message;
     } else {
         if ($message instanceof PollMessage) {
             //Se é uma mensagem de enquete, inicia a enquete e computa o voto
             if (!isset(self::$polls[$message->number])) {
                 self::$polls[$message->number] = new Poll($message->number);
             }
             $poll = self::$polls[$message->number];
             $poll->addVote($cache['nickname'], $message->value);
             $message = new PollResultMessage($poll);
         } else {
             if ($message instanceof RaffleMessage) {
                 //Se é uma mensagem de sorteio, inicia a enquete e computa o voto
                 $usersToSort = [];
                 foreach (self::$connections as $anotherConnection) {
                     if ($anotherConnection !== $connection) {
                         $cache = \Cache::get($anotherConnection->session);
                         if (!isset($usersToSort[$cache['nickname']]) and $cache['mode'] != 'presenter') {
                             $usersToSort[$cache['nickname']] = $anotherConnection;
                         }
                     }
                 }
                 $winnerPointer = array_rand($usersToSort);
                 $winnerCache = \Cache::get($usersToSort[$winnerPointer]->session);
                 Log::d('Winner: ' . $winnerCache['nickname']);
                 $resultMessage = new RaffleResultMessage($winnerCache['nickname'], $winnerCache['name']);
                 foreach (self::$connections as $anotherConnection) {
                     if ($anotherConnection !== $usersToSort[$winnerPointer]) {
                         Sender::send($resultMessage, $anotherConnection);
                     }
                 }
                 $resultMessage->winner = true;
                 Sender::send($resultMessage, $usersToSort[$winnerPointer]);
             }
         }
     }
     if ($message instanceof PollResultMessage) {
         Log::d('Broadcast to everybody');
         //Broadcast para todos
         foreach (self::$connections as $anotherConnection) {
             Sender::send($message, $anotherConnection);
         }
     } else {
         if ($message instanceof PingMessage) {
             Log::d('Send back');
             Sender::send($message, $connection);
         } else {
             if (!$message instanceof RaffleResultMessage) {
                 Log::d('Broadcast to others');
                 //Broadcast para todos, menos para quem enviou
                 foreach (self::$connections as $anotherConnection) {
                     if ($anotherConnection !== $connection) {
                         Sender::send($message, $anotherConnection);
                     }
                 }
             }
         }
     }
     Log::d($jsonMessage . ' - ' . $cache['nickname'] . ' - ' . time());
 }
Exemplo n.º 2
0
 /**
  * Função executada quando o servidor recebe uma mensagem
  *
  * @param ConnectionInterface $connection
  * @param String $jsonMessage
  */
 public function onMessage(ConnectionInterface $connection, $jsonMessage)
 {
     $cache = \Cache::get($connection->session);
     //Envia a mensagem para o MessageManager, ele identificará qual o tipo
     //dela
     $message = MessageManager::createMessage($jsonMessage, $connection);
     if ($message instanceof SlideMessage) {
         Controller::$lastMessage = $message;
     } else {
         if ($message instanceof PollMessage) {
             //Se é uma mensagem de enquete, inicia a enquete e computa o voto
             if (!isset(self::$polls[$message->number])) {
                 self::$polls[$message->number] = new Poll($message->number);
             }
             $poll = self::$polls[$message->number];
             $poll->addVote($message->value);
             $message = new PollResultMessage($poll);
         }
     }
     if ($message instanceof PollResultMessage) {
         Log::d('Broadcast to everybody');
         //Broadcast para todos
         foreach (self::$connections as $anotherConnection) {
             Sender::send($message, $anotherConnection);
         }
     } else {
         if ($message instanceof PingMessage) {
             Log::d('Send back');
             Sender::send($message, $connection);
         } else {
             Log::d('Broadcast to others');
             //Broadcast para todos, menos para quem enviou
             foreach (self::$connections as $anotherConnection) {
                 if ($anotherConnection !== $connection) {
                     Sender::send($message, $anotherConnection);
                 }
             }
         }
     }
     Log::d($jsonMessage . ' - ' . $cache['nickname'] . ' - ' . time());
 }