Advertisement
toorr2p

Untitled

Jan 19th, 2024
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2. try {
  3.     $ws = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  4.  
  5.     if ($ws === false) {
  6.         throw new Exception('socket_create() failed: '.socket_strerror(socket_last_error())."\n");
  7.     }else{
  8.  
  9.         $host_ip = gethostbyname('0.0.0.0');
  10.         socket_bind($ws, $host_ip, 2222);
  11.  
  12.         socket_listen($ws, 5);
  13.  
  14.         $connection = @socket_accept($ws);
  15.  
  16.         //read from websocket
  17.         printf("< %s\n", socket_read($connection, 1000, PHP_NORMAL_READ));
  18.  
  19.         //send to client
  20.         $data = "{var:server_value}\n";
  21.         $byteOut = socket_write($connection, $data, strlen($data));
  22.  
  23.         if ($byteOut === false) {
  24.             echo " Ошибка.";
  25.             throw new Exception('socket_send() failed: '.socket_strerror(socket_last_error())."\n");        
  26.         } else {            
  27.             echo "отправлено " . $byteOut . " байт.\n";
  28.         }
  29.        
  30.     }
  31. }catch (Exception $e) {        
  32.     echo "\nError: ".$e->getMessage();
  33. }
  34. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement