----server.php----
require_once 'Chat.php';
$chat=new Chat();
$socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_option($socket, SOL_SOCKET,SO_REUSEADDR, 1);
socket_bind($socket,0,8090);
socket_listen($socket);
while(true){
$newSocket=socket_accept($socket);
$header=socket_read($newSocket,1024);
$chat->sendHeaders($header,$newSocket,'185.174.172.91:8090/test/ws',8090);
}
socket_close($socket);
-----Chat.php-----
class Chat{
public function sendHeaders($headers_text,$newSocket,$host,$port){
$headers=array();
$tmpLine=preg_split('/\r\n/',$headers_text);
foreach($tmpLine as $line){
$line=rtrim($line);
if(preg_match('/\A(\S+): (.*)\z/',$line,$matches)){
$headers[$matches[1]]=$matches[2];
}
if(array_key_exists('Sec-WebSocket-Key',$headers)){
$key=$headers['Sec-WebSocket-Key'];
$sKey=base64_encode(pack('H*',sha1($key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$strHeader="HTTP/1.1 101 Switching Protocols \r\n".
"Upgrade: WebSocket\r\n".
"Connection: Upgrade\r\n".
"WebSocket-Origin: $host\r\n".
"WebSocket-Location: ws://$host:$port/chat/server.php\r\n".
"Sec-WebSocket-Accept:$sKey\r\n\r\n";
socket_write($newSocket, $strHeader, strlen($strHeader));
}
}
}
}
php -f server.php
Получаю