Websocket giving error in creation

Asked

Viewed 46 times

2

I’m having trouble, while trying to run the websocket, it returns me the following error in the image

inserir a descrição da imagem aqui

the code I’m using is this:

<?php
$ip = 'localhost';
$porta = 8080;
//-------------------------------
$servidor = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($servidor, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($servidor, $ip, $porta);
socket_listen($servidor);
$cliente = socket_accept($servidor);
//--------------------------------
$pedido = socket_read($cliente, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $pedido, $matches);
$key = base64_encode(pack(
    'H*',
    sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($cliente, $headers, strlen($headers));
// mensagem
while (true) {
    sleep(1);
    $content = 'horas: ' . time();
    $response = chr(129) . chr(strlen($content)) . $content;
    socket_write($cliente, $response);
}
?>

Note: I am trying to learn how to use kkk alone

1 answer

2


It’s embarrassing, but I forgot to enable in PHP.ini the "Extension=sockets", after I took the point and comma and restarted the apache server, worked normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.