/** * staring application * creating rooms and handling socket connections * @param $socket Socket_Server * */ public function startApplication(Socket_Server $socket) { $userManager = new Manager_User(); $clientManager = new Manager_Client(); $roomManager = new Manager_Room(); $roomManager->add("main", new RoomItems_DefaultRoom()); $clientManager->setMaxCount(10); $sock = $socket->getSocketInstance(); //this is loop that runs server while (true) { // Setup clients listen socket for reading $read = array(); //adding server at first place in read array array_push($read, $sock); //looping throught all socket connections foreach ($clientManager as $c) { array_push($read, $c->getSocketInstance()); } $value = null; //wraper for socket_select $ready = $socket->select($read, $value, $value, 100); //if new socket is added if (in_array($sock, $read)) { //create Socket_Client instance $clientObject = new Socket_Client(); //set socket resource to Socket_Client $clientObject->setSocketInstance($socket->accept()); //adding Socket_Client to clientManager $clientManager->add($clientObject); //creating new user with Default_User object $userSession = $userManager->add(new UserItems_DefaultUser(), $clientObject); //adding new user to main room $roomManager->addUserToRoom($userManager->getUserBySession($userSession), "main"); $key = array_search($sock, $read); //unset this key from read unset($read[$key]); } // loop through all the clients that have data to read from foreach ($read as $readSock) { //read sockets $client = $clientManager->getItemByResource($readSock); //read message $data = $client->read(1024); // check if the client is disconnected if ($data === false) { unset($c); continue; } // trim off the trailing/beginning white spaces $data = trim($data); //parse data and dispatch events to rooms if (!empty($data)) { $recive = new Communication_Recive($data); $roomEvents = $recive->getParsedRoomsEvents(); $roomManager->dispatchRoomEvents($roomEvents); } } } }
/** * adding new socket client * @param $socketClient Socket_Client */ public function add(Socket_Client $socketClient) { $unique = false; while ($unique == false) { $session = Session_Session::generate(); foreach ($this->items as $items) { if ($items->getSession() == $session) { continue; } } $unique = true; } $socketClient->setSession($session); $this->items[$socketClient->getSocketInstance()] = $socketClient; }
private static function initClientFactories() { self::tryRegisterClientFactory("http", "\\Hprose\\Http\\Client"); self::tryRegisterClientFactory("https", "\\Hprose\\Http\\Client"); self::tryRegisterClientFactory("tcp", "\\Hprose\\Socket\\Client"); self::tryRegisterClientFactory("ssl", "\\Hprose\\Socket\\Client"); self::tryRegisterClientFactory("sslv2", "\\Hprose\\Socket\\Client"); self::tryRegisterClientFactory("sslv3", "\\Hprose\\Socket\\Client"); self::tryRegisterClientFactory("tls", "\\Hprose\\Socket\\Client"); self::tryRegisterClientFactory("unix", "\\Hprose\\Socket\\Client"); self::$clientFactoriesInited = true; }
public function __construct($uris = null, $async = true) { parent::__construct($uris, $async); $this->hdtrans = new HalfDuplexTransporter($this, $async); $this->fdtrans = new FullDuplexTransporter($this, $async); }