A
A
AndRussia2021-08-01 20:02:52
PHP
AndRussia, 2021-08-01 20:02:52

Why is the page not loading?

Hello, I decided to study web sockets. I found an article on the Internet, used the code - the page does not load. Used code from other sites - all the same. The page does not want to load. I am using OpenServer. What can be wrong?
Articles:
https://habr.com/ru/post/209864/
https://petukhovsky.com/simple-web-socket-on-php-f...
Here is an example of code that does not load the page:

<?php

$socket = stream_socket_server("tcp://127.0.0.1:80", $errno, $errstr);

if (!$socket) {
    die("$errstr ($errno)\n");
}

$connects = array();
while (true) {
    //формируем массив прослушиваемых сокетов:
    $read = $connects;
    $read []= $socket;
    $write = $except = null;

    if (!stream_select($read, $write, $except, null)) {//ожидаем сокеты доступные для чтения (без таймаута)
        break;
    }

    if (in_array($socket, $read)) {//есть новое соединение
        $connect = stream_socket_accept($socket, -1);//принимаем новое соединение
        $connects[] = $connect;//добавляем его в список необходимых для обработки
        unset($read[ array_search($socket, $read) ]);
    }

    foreach($read as $connect) {//обрабатываем все соединения
        $headers = '';
        while ($buffer = rtrim(fgets($connect))) {
            $headers .= $buffer;
        }
        fwrite($connect, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nПривет");
        fclose($connect);
        unset($connects[ array_search($connect, $connects) ]);
    }
}

fclose($server);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-08-02
@AndRussia

Because the web socket server needs to be started from the console command, and not through OpenServer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question