I can’t connect to the server - Websockt

Asked

Viewed 45 times

0

I am not able to connect to my Websockt server always error can anyone help me ? Server - PHP

<?php
 $host = 'localhost';
    $port = '1234';
    $null = NULL;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, 'localhost');
socket_listen($socket);
socket_accept($socket);
$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '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:$port/deamon.php\r\n".
"Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_conn,$upgrade,strlen($upgrade));
//Unmask incoming framed message

function unmask($text) {
    $length = ord($text[1]) & 127;
    if($length == 126) {
        $masks = substr($text, 4, 4);
        $data = substr($text, 8);
    }
    elseif($length == 127) {
        $masks = substr($text, 10, 4);
        $data = substr($text, 14);
    }
    else {
        $masks = substr($text, 2, 4);
        $data = substr($text, 6);
    }
    $text = "";
    for ($i = 0; $i < strlen($data); ++$i) {
        $text .= $data[$i] ^ $masks[$i%4];
    }
    return $text;
}

//Encode message for transfer to client.
function mask($text)
{
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);

    if($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif($length > 125 && $length < 65536)
        $header = pack('CCn', $b1, 126, $length);
    elseif($length >= 65536)
        $header = pack('CCNN', $b1, 127, $length);
    return $header.$text;
}

Javascript

<script>
      var connection = new WebSocket('ws://localhost:1234');

</script>

error:

Websocket Connection to 'Ws://localhost:1234/' failed: Error in Connection establishment: net:ERR_CONNECTION_REFUSED

inserir a descrição da imagem aqui

  • 1

    You left running the PHP script before connecting?

  • @Andersoncarloswoss yes I left

  • Tested at door 80 or 8080?

  • @Papacharlie yes I tried and gives the same mistake

  • I crawled to get it, I did it in the 8080. I remember some comments relating to Firewall, take a look. When you start from the command line some error appears?

No answers

Browser other questions tagged

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