'message', 'data' => $decoded_data])); } foreach ($clients as $client) { if ($client != $socket && $client != $client_socket) { @socket_write($client, $response, strlen($response)); } } } } } function perform_handshake($receved_header, $client_conn, $host, $port) { $headers = array(); $lines = preg_split("/\r\n/", $receved_header); foreach ($lines as $line) { $line = chop($line); if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) { $headers[$matches[1]] = $matches[2]; } } if (!isset($headers['Sec-WebSocket-Key'])) return; $secKey = $headers['Sec-WebSocket-Key']; $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))); $upgrade = "HTTP/1.1 101 Switching Protocols\r\n" . "Upgrade: websocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Accept: $secAccept\r\n\r\n"; socket_write($client_conn, $upgrade, strlen($upgrade)); } function unmask($text) { if (strlen($text) < 2) return null; $length = ord($text[1]) & 127; if ($length == 126) { if (strlen($text) < 8) return null; $masks = substr($text, 4, 4); $data = substr($text, 8); } elseif ($length == 127) { if (strlen($text) < 14) return null; $masks = substr($text, 10, 4); $data = substr($text, 14); } else { if (strlen($text) < 6) return null; $masks = substr($text, 2, 4); $data = substr($text, 6); } $decoded = ""; for ($i = 0; $i < strlen($data); ++$i) { $decoded .= $data[$i] ^ $masks[$i % 4]; } return $decoded; } function mask($text) { $b1 = 0x81; // FIN + Opcode 1 (text) $length = strlen($text); if ($length <= 125) $header = pack('CC', $b1, $length); elseif ($length > 125 && $length < 65536) $header = pack('CCn', $b1, 126, $length); elseif ($length >= 65536) $header = pack('CCNN', $b1, 127, $length); return $header . $text; }