Пример #1
0
 public function testRemoveUserFromChat()
 {
     $this->cm->addUser($this->rid, $this->userId);
     $chat = $this->cm->findChat($this->chatId, $this->rid);
     $this->cm->removeUserFromChat($this->rid);
     $this->assertEquals(0, sizeof($chat->getUsers()), 'Chat should not contains any users');
 }
Пример #2
0
 /**
  * Process auth request. Find user chat(if not exists - create it)
  * and send message to all other clients
  *
  * @access private
  * @param $rid
  * @param $data
  * @return void
  */
 private function authRequest($rid, array $data)
 {
     $chatId = $data['cid'];
     Yii::info('Auth request from rid: ' . $rid . ' and chat: ' . $chatId, 'chat');
     $userId = !empty($data['user']['id']) ? $data['user']['id'] : '';
     //the same user already connected to current chat, need to close old connect
     if ($oldRid = $this->cm->isUserExistsInChat($userId, $chatId)) {
         $this->closeRequest($oldRid);
     }
     $this->cm->addUser($rid, $userId, $data['user']);
     $chat = $this->cm->findChat($chatId, $rid);
     $users = $chat->getUsers();
     $joinedUser = $this->cm->getUserByRid($rid);
     $response = ['user' => $joinedUser, 'join' => true];
     foreach ($users as $user) {
         //send message for other users of this chat
         if ($userId != $user->getId()) {
             $conn = $this->clients[$user->getRid()];
             $conn->send(Json::encode(['type' => 'auth', 'data' => $response]));
         }
     }
     //send auth response for joined user
     $response = ['user' => $joinedUser, 'users' => $users, 'history' => $this->cm->getHistory($chat->getUid())];
     $conn = $this->clients[$rid];
     $conn->send(Json::encode(['type' => 'auth', 'data' => $response]));
 }