Guest User

Untitled

a guest
Jul 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. <?php
  2. /* < < < < < < < < -< MODULES >- > > > > > > > > */
  3. require_once("classes/chat.php");
  4. $chat = new Chat();
  5. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  6.  
  7. /* < < < < < < < < -< VARIABLES >- > > > > > > > */
  8. $url = 'localhost/chat';
  9. $port = 8091;
  10. $ip = 0;
  11.  
  12. /* < < < < < < < < -< CONNECTED >- > > > > > > > */
  13. socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
  14. socket_bind($socket, $ip, $port);
  15. socket_listen($socket);
  16.  
  17. $clientSocketArray = array($socket);
  18. $null = null;
  19.  
  20. while(true) {
  21. $newSocketArray = $clientSocketArray;
  22. socket_select($newSocketArray, $null, $null, 0, 10);
  23.  
  24. if(in_array($socket, $newSocketArray)) {
  25. $newSocket = socket_accept($socket);
  26. $clientSocketArray[] = $newSocket;
  27.  
  28. $header = socket_read($newSocket, 1024);
  29. $chat -> sendHeaders($header, $newSocket, $url, $port);
  30.  
  31. socket_getpeername($newSocket, $client_ip_address);
  32. $connectionACK = $chat -> newConnectionACK($client_ip_address);
  33. $chat -> send($connectionACK, $clientSocketArray);
  34. }
  35. }
  36.  
  37. /* < < < < < < < -< CLOSE-CONNECT >- > > > > > > */
  38. socket_close($socket);
  39. ?>
  40.  
  41. <?php
  42. class Chat {
  43. /*<-->*/
  44. public function sendHeaders($headersText, $newSocket, $host, $port) {
  45. $headers = array();
  46. $tmpLine = preg_split("/rn/", $headersText);
  47.  
  48. foreach($tmpLine as $line) {
  49. $line = rtrim($line);
  50. if(preg_match('/A(S+): (.*)z/', $line, $matches)) {
  51. $headers[$matches[1]] = $matches[2];
  52. }
  53. }
  54.  
  55. $key = $headers['Sec-WebSocket-Key'];
  56. $sKey = base64_encode(pack('H*', sha1($key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
  57.  
  58. $strHeadr = "HTTP/1.1 101 Switching Protocolsrn" .
  59. "Upgrade: websocketrn" .
  60. "Connection: Upgradern" .
  61. "WebSocket-Origin: $hostrn" .
  62. "WebSocket-Location: ws://$host:$port/chat/server.phprn" .
  63. "Sec-WebSocket-Accept: $sKeyrnrn"
  64. ;
  65.  
  66. socket_write($newSocket, $strHeadr, strlen($strHeadr));
  67. }
  68. /*<-->*/
  69. public function newConnectionACK($client_ip_address) {
  70. $message = "New client $client_ip_address connected";
  71. $messageArray = [
  72. "text" => $message,
  73. "type" => "newConnectionACK"
  74. ];
  75. $ack = $this->seal(json_encode($messageArray));
  76. return $ack;
  77. }
  78. /*<-->*/
  79. public function seal($socketData) {
  80. $b1 = 0x81;
  81. $lenght = strlen($socketData);
  82. $text = "";
  83.  
  84. if($lenght <= 125) {
  85. $text = pack('CC', $b1, $lenght);
  86. } else if($lenght > 125 && $lenght < 65536) {
  87. $text = pack('CCn', $b1, 126, $lenght);
  88. } else if($lenght > 65535) {
  89. $text = pack('CCNN', $b1, 127, $lenght);
  90. }
  91. return $text.$socketData;
  92. }
  93. /*<-->*/
  94. public function send($message, $clientSocketArray) {
  95.  
  96. $messageLenght = strlen($message);
  97.  
  98. foreach($clientSocketArray as $clientSocket) {
  99. socket_write($clientSocket, $message, $messageLenght);
  100. }
  101. var_export( $clientSocketArray );
  102. return true;
  103. }
  104. /*<-->*/
  105. }
Add Comment
Please, Sign In to add comment