/** * send new Message to room * * @param [array] $data with message */ public function onNew($data) { $validator = new MessageValidator($data); // find room & user $users = $this->service->get('users'); $rooms = $this->service->get('rooms'); $user = UserFinder::set($users)->findOneBySocketId($this->socket->id); $room = RoomFinder::set($rooms)->findOneByUser($user); if ($room && $user && $validator->isValid()) { // set username & convert message $data['id'] = $user->getId(); $data['username'] = $user->getName(); $data['content'] = htmlentities($data['content']); $io = $this->service->get('io'); $io->in($room->getSlug())->emit('message:incoming', array('message' => $data)); } }
/** * user enter room * * @param [type] $data [description] * @param [type] $socket [description] * */ public function onEnter($data) { $validator = new RoomSlugValidator($data); if ($validator->isValid()) { // find room & user $users = $this->service->get('users'); $rooms = $this->service->get('rooms'); $io = $this->service->get('io'); $room = RoomFinder::set($rooms)->findOneBySlug($data['slug']); $user = UserFinder::set($users)->findOneBySocketId($this->socket->id); // if room exists join room and add user to room if ($room && $user) { // find last room and remove user $currentRoom = RoomFinder::set($rooms)->findOneByUser($user); if ($currentRoom) { $this->leaveCurrentRoom($user, $currentRoom); } // join room $this->socket->join($room->getSlug()); $room->addUser($user); // send users of current room $io->in($room->getSlug())->emit('room:update', $room); // send list of rooms $io->emit('room:list', $rooms->toArray()); } } }
/** * update user * * @param [type] $data * @param [type] $socket * */ public function onUpdate($data) { $io = $this->service->get('io'); $users = $this->service->get('users'); $rooms = $this->service->get('rooms'); $validator = new UserValidator($data['user']); if ($validator->isValid()) { $user = UserFinder::set($users)->findOneBySocketId($this->socket->id); $room = RoomFinder::set($rooms)->findOneByUser($user); $data['user']['name'] = htmlentities($data['user']['name']); $user->setName($data['user']['name']); $user->setColor($data['user']['color']); // send user $this->socket->emit('user:change', $user); // send users if user is connected to a room if ($room) { $io->in($room->getSlug())->emit('room:update', $room); } } }