Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function send($client, $msg) {
- $msg = base64_encode($msg);
- $b1 = 0x80 | (0x1 & 0x0f);
- $length = strlen($msg);
- if ($length <= 125)
- $header = pack('CC', $b1, $length);
- elseif ($length > 125 && $length < 65536)
- $header = pack('CCn', $b1, 126, $length);
- else
- $header = pack('CCN', $b1, 127, $length);
- $msg = $header.$msg;
- socket_write($client, $msg, strlen($msg));
- }
- $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- socket_bind($socket, '127.0.0.1', 8000);
- socket_listen($socket);
- $client = socket_accept($socket);
- $handshake = socket_read($client, 1024);
- preg_match('/Sec-WebSocket-Key\: (.+?)\r\n/', $handshake, $accept);
- $accept = base64_encode(sha1("$accept[1]258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
- $handshake = "HTTP/1.1 101 Switching Protocols\r\n";
- $handshake .= "Upgrade: websocket\r\n";
- $handshake .= "Connection: Upgrade\r\n";
- $handshake .= "Sec-WebSocket-Accept: $accept\r\n\r\n";
- socket_write($client, $handshake.chr(0), strlen($handshake.chr(0)));
- send($client, 'hello');
- sleep(5);
- socket_close($client);
- socket_close($socket);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement