Example #1
0
 /**
  * Finds the Document model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Document the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Document::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #2
0
File: Socket.php Project: xddpx/io
 public function listen()
 {
     try {
         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
         //Create TCP/IP sream socket
         socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
         //reuseable port
         socket_bind($socket, 0, $this->getPort());
         //bind socket to specified host
         socket_listen($socket);
         //listen to port
         $this->clients = array($socket);
         //create & add listning socket to the list
         //start endless loop, so that our script doesn't stop
         while (true) {
             $changed = $this->clients;
             //manage multipal connections
             socket_select($changed, $null, $null, 0, 10);
             //returns the socket resources in $changed array
             //check for new socket
             if (in_array($socket, $changed)) {
                 $socket_new = socket_accept($socket);
                 //accpet new socket
                 $this->clients[] = $socket_new;
                 //add socket to client array
                 $header = socket_read($socket_new, 1024);
                 //read data sent by the socket
                 $this->performHandshaking($header, $socket_new, $this->getHost(), $this->getPort());
                 //perform websocket handshake
                 socket_getpeername($socket_new, $ip);
                 //get ip address of connected socket
                 $response = $this->mask(json_encode(array('type' => 'system', 'message' => $ip . ' connected')));
                 //prepare json data
                 $this->sendMessage($response);
                 //notify all users about new connection
                 $found_socket = array_search($socket, $changed);
                 //make room for new socket
                 unset($changed[$found_socket]);
             }
             //loop through all connected sockets
             foreach ($changed as $changed_socket) {
                 //                    check for any incomming data
                 while (socket_recv($changed_socket, $buf, 8192000, 0) >= 1) {
                     $received_text = $this->unmask($buf);
                     //unmask data
                     $tst_msg = json_decode($received_text);
                     //json decode
                     $diff = $tst_msg->diff;
                     //document diff
                     $auth_key = $tst_msg->auth_key;
                     //user auth key
                     $document_id = $tst_msg->document_id;
                     //document id
                     if (!Yii::$app->db->getIsActive()) {
                         Yii::$app->db->open();
                     }
                     $user = User::findIdentityByAuthKey($auth_key);
                     if ($user === NULL) {
                         $response_text = $this->mask(json_encode(array('type' => 'system', 'message' => "You don't have permission to access")));
                         $this->sendMessage($response_text);
                         //send data
                         break 2;
                     }
                     $document = Document::findOne(['id' => intval($document_id)]);
                     if ($document === NULL) {
                         $response_text = $this->mask(json_encode(array('type' => 'system', 'message' => "The requested document does not exist.")));
                         $this->sendMessage($response_text);
                         //send data
                         break 2;
                     }
                     $this->saveDocument($document, $diff);
                     //prepare data to be sent to client
                     $response_text = $this->mask(json_encode(array('type' => 'update', 'document_diff' => $diff)));
                     var_dump($this->sendMessage($response_text));
                     //send data
                     break 2;
                     //exist this loop
                 }
                 $buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
                 if ($buf === false) {
                     // check disconnected client
                     // remove client for $this->clients array
                     $found_socket = array_search($changed_socket, $this->clients);
                     @socket_getpeername($changed_socket, $ip);
                     unset($this->clients[$found_socket]);
                     //notify all users about disconnected connection
                     $response = $this->mask(json_encode(array('type' => 'system', 'message' => $ip . ' disconnected')));
                     $this->sendMessage($response);
                 }
             }
         }
     } catch (\yii\base\Exception $e) {
     } finally {
         // close the listening socket
         socket_close($socket);
     }
 }