I have a one problem, I have a page called server.php and another client.php, the client code is as follows:
<script src="scripts/jQuery/jquery-3.2.1.min.js"></script>
<script>
var socket = new WebSocket('ws://127.0.0.1:4000');
socket.onopen = function () {
}
socket.onmessage = function(){
alert("Chegou mensagem");
}
And the server.php is
<?php
ob_implicit_flush();
set_time_limit(0);
$host = "127.0.0.1";
$porta = 4000;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $host, $porta);
socket_listen($socket);
echo "Esperando conexão com a porta: ".$porta;
$con = false;
switch(socket_select($r=array($socket),$w=array($socket),$e=array($socket),30)){
case 0:
echo "Tempo de espera terminou";
break;
case 1:
echo "Conectado na porta: ".$porta;
$con = socket_accept($socket);
break;
case 2:
echo "Não foi possível se conectar!";
break;
}
socket_recv($con,$resposta,1024,MSG_WAITALL);
if(preg_match("/Sec-WebSocket-Key:(.*)\==/i", $resposta, $rep)){
$key = $rep[1]."==";
}
$chave = base64_encode(sha1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n".
"Upgrade: websocket\r\n".
"Connection: Upgrade\r\n".
"WebSocket-Origin: $host\r\n".
"WebSocket-Location: ws://$host:$porta/\r\n".
"Sec-WebSocket-Accept:$chave\r\n\r\n";
if(socket_write($socket, $upgrade, strlen($upgrade))){
echo "Protocolo foi enviado";
}else{
echo socket_strerror(socket_last_error());
}
?>
The problem is that the socket_write () function does not send the socket to the client, should not the message appear on the "message arrived" client page?