How to connect a websocket?

Trying to configure the websocket. In js just create the socket = new WebSocket("wss://3d-mitra.ru/test.php");
On the server side, ordered the opening of the socket
define('PORT',"8080");
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//create a socket
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);//allow the use of one port for multiple connections
socket_bind($socket, "92.53.96.36" , PORT);//bind it to the specified ip and port

socket_listen($socket);//listening socket

// echo socket_strerror(socket_last_error($socket));
while (true) {
 $newSocket = socket_accept($socket); //echo "ok";
var_dump($newSocke);
 $header = socket_read($socket, 1024);
 sendHeaders($header,$newSocket,'3d-mitra.ru', PORT);
}
socket_close($socket);

function sendHeaders($hederText,$newSocket,$host,$port){
 $headers = array();
 $tmpLine = preg_split('/\r\n/', $hederText);
 foreach ($tmpLine as $line) {
 $line = rtrim($line);
 if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
 $headers[$matches[1]] = $matches[2];

}
}
 $sKey = base64_encode(pack('H*',sha1($headers['Sec-WebSocket-Key'].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))) ;
 $strHeader = "HTTP/1.1 101 Switching Protocols\r\n" .
 "Upgrade: websocket\r\n" .
 "Connection: Upgrade\r\n" .
 "Sec-WebSocket-Extensions: deflate-frame\r\n".
 "Sec-WebSocket-Protocol: soap\r\n".
 "Sec-WebSocket-Origin: $host".
 "Sec-WebSocket-Location: wss://$host/test.php\r\n".
"Sec-WebSocket-Accept:$sKey\r\n\r\n";
 socket_write($newSocket,$strHeader, strlen($strHeader));
}

Via ssh run the script, errors does not produce, the port is listening.
But connect does not work, falls in the 502 error when trying to connect
April 4th 20 at 00:50
2 answers
April 4th 20 at 00:52
Use the library workerman to raise a normal socket in php. Your option will not work. For wss need valid certificates or proxy on nginx
I have a real server this script is run. There are certificates. Wss or need something in a special way to connect, not enough running https? - Zander commented on April 4th 20 at 00:55
To soedineniya it wss necessary when you create a transfer context with the location of the certificate, etc. to use your work, but not celesoobrazno. I recommend to watch Workerman there is the same principle but without the leaks and in my tests very good results in performance. - Linda.Watsi commented on April 4th 20 at 00:58
April 4th 20 at 00:54
Something tells me that there: $header = socket_read($socket, 1024); need to read from $newSocket
But connect does not work, falls in the 502 error when trying to connect
502 usually means that nginx (or that You as a reverse proxy) can not connect to the backend.

Check if you can connect telnet'Ohm to php server from the machine where nginx can banal firewall will not let me.
Check that is correctly written to config proxy
This script runs on the hosting, there all right should be spelled out. Proxying is not used. 502 error I have is due to the fact that a response is not received before the expiration of the TTL, the script just does not respond. - Zander commented on April 4th 20 at 00:57

Find more questions by tags PHP