Example #1
0
 public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg)
 {
     $this->say("[AJXP] Admin message received!");
     // Echo
     // $user->sendMessage($msg);
     $data = unserialize($msg->getData());
     $repoId = $data["REPO_ID"];
     $userId = isset($data["USER_ID"]) ? $data["USER_ID"] : false;
     $userGroupPath = isset($data["GROUP_PATH"]) ? $data["GROUP_PATH"] : false;
     $msg->setData($data["CONTENT"]);
     foreach ($this->getConnections() as $conn) {
         if ($conn == $user) {
             continue;
         }
         if ($repoId != "AJXP_REPO_SCOPE_ALL" && (!isset($conn->currentRepository) || $conn->currentRepository != $repoId)) {
             $this->say("Skipping, not the same repository");
             continue;
         }
         if ($userId !== false && $conn->ajxpId != $userId) {
             $this->say("Skipping, not the same userId");
             continue;
         }
         if ($userGroupPath != false && (!isset($conn->ajxpGroupPath) || $conn->ajxpGroupPath != $userGroupPath)) {
             $this->say("Skipping, not the same groupPath");
             continue;
         }
         $this->say("Should dispatch to user " . $conn->ajxpId);
         $conn->sendMessage($msg);
     }
     //$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
     //$user->sendFrame($frame);
 }
 public function sendMessage(IWebSocketMessage $msg)
 {
     foreach ($msg->getFrames() as $frame) {
         if ($this->sendFrame($frame) === false) {
             return FALSE;
         }
     }
     return TRUE;
 }
Example #3
0
 public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg)
 {
     $this->say("[ECHO] " . strlen($msg->getData()) . " bytes");
     // Echo
     $user->sendMessage($msg);
 }
 public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg)
 {
     $this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'");
 }
 public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg)
 {
     $msg->setData("Logging this: " . $msg->getData());
     $this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'.  ");
 }
Example #6
0
 public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg)
 {
     $this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'");
     $message = $msg->getData();
     if (Common::startsWith($message, 'XLIN')) {
         $this->onLogin($user, $message);
     } elseif (false !== ($dog_user = $this->isLoggedIn($user))) {
         $this->addToQueue($user, $dog_user, $message);
     } else {
         $this->say("XLIN4");
     }
 }
 public function onMessage(IWebSocketConnection $conn, IWebSocketMessage $msg)
 {
     $arr = json_decode($msg->getData(), true);
     // If this is a new websocket connection, handle the user up front
     if ($arr['messageType'] == 'myid') {
         if (isset($this->users[$arr['id']])) {
             $user = $this->users[$arr['id']];
         } else {
             $user = new Player(gentoken(), $conn->getId());
         }
         $this->users[$user->id()] = $user;
         $this->conns[$conn->getId()] = $user;
         $user->setConnection($conn);
         $user->send('myname', array('name' => $user->name(), 'id' => $user->id(), 'ingame' => $user->game() != null));
         if ($user->game() != null) {
             if ($user->game()->started) {
                 $user->rejoinGame();
             } else {
                 $user->rejoinWaitingRoom();
             }
         } else {
             foreach ($this->games as $game) {
                 if ($game->started) {
                     continue;
                 }
                 $user->send('newgame', array('name' => $game->name, 'creator' => $game->creator->name(), 'id' => $game->id));
             }
         }
         $this->say("{$user->id()} connected");
         return;
     }
     // Otherwise we better have a user set for them, and then continue on
     // as normally when processing the message
     if (!isset($this->conns[$conn->getId()])) {
         return;
     }
     $user = $this->conns[$conn->getId()];
     switch ($arr['messageType']) {
         case 'newgame':
             if ($user->game() != null) {
                 return;
             }
             // ERRORS NOT SHOWING ON CLIENT: FIX FIX FIX
             if ($arr['name'] == '') {
                 return $user->send('error', 'Game needs a valid name');
             }
             $game = new SevenWonders();
             $game->maxplayers = intval($arr['players']);
             $game->name = $arr['name'];
             $game->id = gentoken();
             $game->server = $this;
             $game->addPlayer($user);
             $this->games[$game->id] = $game;
             if ($game->maxplayers > 1) {
                 $this->broadcast('newgame', array('name' => $game->name, 'creator' => $game->creator->name(), 'id' => $game->id), $user);
             }
             break;
         case 'joingame':
             if ($user->game() != null) {
                 break;
             }
             $id = $arr['id'];
             if (!isset($this->games[$id]) || $this->games[$id]->started) {
                 break;
             }
             $this->games[$id]->addPlayer($user);
             break;
         case 'changename':
             if ($user->game() == null && $arr['name'] != '') {
                 $user->setName($arr['name']);
             }
             // Broadcast name change here in case they're hosting a game?
             break;
         default:
             if ($user->game() != null) {
                 $user->game()->onMessage($user, $arr);
             } else {
                 $user->send('error', "Error: could not recognize command " . $arr['messageType']);
             }
             break;
     }
 }
 /**
  * Dispatch incoming message to the associated resource and to the general onMessage event handler
  * @param IWebSocketUser $user
  * @param IWebSocketMessage $msg
  */
 protected function dispatchMessage(IWebSocketConnection $user, IWebSocketMessage $msg)
 {
     $this->debug("dispatchMessage from {$user->getIp()}: {$msg->getData()}");
     if (array_key_exists($this->_connections[$user], $this->uriHandlers)) {
         $this->uriHandlers[$this->_connections[$user]]->onMessage($user, $msg);
     }
     foreach ($this->_observers as $o) {
         $o->onMessage($user, $msg);
     }
 }