protected function _handshake($connectionId)
{
//read the headers from the connection
if (!strpos($this->_read[$connectionId], "\r\n\r\n")) {
return true;
}
preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $this->_read[$connectionId], $match);
if (empty($match[1])) {
return false;
}
$headers = explode("\r\n", $this->_read[$connectionId]);
$info = array();
foreach ($headers as $header) {
if (($explode = explode(':', $header)) && isset($explode[1])) {
$info[trim($explode[0])] = trim($explode[1]);
} elseif (($explode = explode(' ', $header)) && isset($explode[1])) {
$info[$explode[0]] = $explode[1];
}
}
/*$source = explode(':', stream_socket_get_name($this->clients[$connectionId], true));
$info['Ip'] = $source[0];*/
$this->_read[$connectionId] = '';
//send a header according to the protocol websocket
$SecWebSocketAccept = base64_encode(pack('H*', sha1($match[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" . "Upgrade: websocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Accept:{$SecWebSocketAccept}\r\n\r\n";
$this->_write($connectionId, $upgrade);
unset($this->_handshakes[$connectionId]);
$this->onOpen($connectionId, $info);
return true;
}