Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3. define('HOST',"localhost");
  4. define('PORT',"9000");
  5.  
  6. //Create TCP/IP sream socket
  7. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  8.  
  9. //reuseable port
  10. socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
  11.  
  12. //bind socket to specified host
  13. socket_bind($socket, 0, PORT);
  14.  
  15. //listen to port
  16. socket_listen($socket);
  17.  
  18. //create & add listning socket to the list
  19. $clients = array($socket);
  20.  
  21. //start endless loop, so that our script doesn't stop
  22. while (true) {
  23.  
  24.     //manage multipal connections
  25.     $changed = $clients;
  26.  
  27.     //returns the socket resources in $changed array
  28.     socket_select($changed, $null, $null, 0, 10);
  29.    
  30.     //check for new socket
  31.     if (in_array($socket, $changed)) {
  32.         $socket_new = socket_accept($socket); //accpet new socket
  33.         $clients[] = $socket_new; //add socket to client array
  34.        
  35.         $header = socket_read($socket_new, 1024); //read data sent by the socket
  36.         ** perform_handshaking($header, $socket_new, HOST, PORT); //perform websocket handshake Validation and acl etc ..
  37.        
  38.         socket_getpeername($socket_new, $ip); //get ip address of connected socket
  39.         $response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' connected'))); //prepare json data
  40.         send_message($response); //notify all users about new connection
  41.        
  42.         //make room for new socket
  43.         $found_socket = array_search($socket, $changed);
  44.         unset($changed[$found_socket]);
  45.     }
  46.    
  47.     //loop through all connected sockets
  48.     foreach ($changed as $changed_socket) {
  49.        
  50.         //check for any incomming data
  51.         while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
  52.         {
  53.             $received_text = unmask($buf); //unmask data
  54.             $tst_msg = json_decode($received_text); //json decode
  55.             $user_name = $tst_msg->name; //sender name
  56.             $user_message = $tst_msg->message; //message text
  57.             $user_color = $tst_msg->color; //color
  58.            
  59.             //prepare data to be sent to client
  60.             $response_text = mask(json_encode(array('type'=>'usermsg', 'name'=>$user_name, 'message'=>$user_message, 'color'=>$user_color)));
  61.             send_message($response_text); //send data
  62.             break 2; //exist this loop
  63.         }
  64.        
  65.         $buf = socket_read($changed_socket, 1024, PHP_NORMAL_READ);
  66.  
  67.     // check disconnected client
  68.         if ($buf === false) {
  69.      
  70.             // remove client for $clients array
  71.             $found_socket = array_search($changed_socket, $clients);
  72.             socket_getpeername($changed_socket, $ip);
  73.             unset($clients[$found_socket]);
  74.            
  75.             //notify all users about disconnected connection
  76.             $response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' disconnected')));
  77.             send_message($response);
  78.         }
  79.     }
  80. }
  81.  
  82. // close the listening socket
  83. socket_close($sock);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement