This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: iiddaannyy on Aug 23rd, 2012  |  syntax: PHP  |  size: 1.07 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function send($client, $msg) {
  2.         $msg = base64_encode($msg);
  3.         $b1 = 0x80 | (0x1 & 0x0f);
  4.         $length = strlen($msg);
  5.         if ($length <= 125)
  6.                 $header = pack('CC', $b1, $length);
  7.         elseif ($length > 125 && $length < 65536)
  8.                 $header = pack('CCn', $b1, 126, $length);
  9.         else
  10.                 $header = pack('CCN', $b1, 127, $length);
  11.         $msg = $header.$msg;
  12.         socket_write($client, $msg, strlen($msg));
  13. }
  14. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  15. socket_bind($socket, '127.0.0.1', 8000);
  16. socket_listen($socket);
  17. $client = socket_accept($socket);
  18. $handshake = socket_read($client, 1024);
  19. preg_match('/Sec-WebSocket-Key\: (.+?)\r\n/', $handshake, $accept);
  20. $accept = base64_encode(sha1("$accept[1]258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
  21. $handshake = "HTTP/1.1 101 Switching Protocols\r\n";
  22. $handshake .= "Upgrade: websocket\r\n";
  23. $handshake .= "Connection: Upgrade\r\n";
  24. $handshake .= "Sec-WebSocket-Accept: $accept\r\n\r\n";
  25. socket_write($client, $handshake.chr(0), strlen($handshake.chr(0)));
  26. send($client, 'hello');
  27. sleep(5);
  28. socket_close($client);
  29. socket_close($socket);
clone this paste RAW Paste Data