簡體   English   中英

瀏覽器無法連接到php websocket服務器

[英]Browser won't connect to php websocket server

我想做的是設置一個php websocket服務器來運行聊天應用程序。 我嘗試使用有效的Ratchet php,可以啟動並運行一個簡單的聊天應用程序,但是我意識到它不符合我的特定需求。

我的開發環境是在ubuntu vm Homestead盒上設置的。

我要從這個站點舉一個例子。 我已將地址和端口更改為我的站點。 當我啟動服務器時,它運行良好,並且可以使用telnet進行連接。 但是,當我通過telnet發送消息時,握手失敗,並且斷開連接。

這是php websocket服務器代碼(注釋是給我的,因此我可以嘗試更好地了解發生了什么事情):

#!/usr/local/bin/php
<?php
$port = 8080;

// create a streaming socket, of type TCP/IP
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// set the option to reuse the port
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

// "bind" the socket to the address to "localhost", on port $port
// so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
socket_bind($sock, 'example.app', $port);

// start listen for connections
socket_listen($sock);

// create a list of all the clients that will be connected to us..
// add the listening socket to this list
$clients = array($sock);

while (true) {
    // create a copy, so $clients doesn't get modified by socket_select()
    $read = $clients;

    // get a list of all the clients that have data to be read from
    // if there are no clients with data, go to next iteration
    if (socket_select($read, $write = NULL, $except = NULL, 0) < 1)
        continue;

    // check if there is a client trying to connect
    if (in_array($sock, $read)) {
        // accept the client, and add him to the $clients array
        $clients[] = $newsock = socket_accept($sock);

        // send the client a welcome message
        socket_write($newsock, "no noobs, but ill make an exception :)\n" .
                "There are " . (count($clients) - 1) . " client(s) connected to the server\n");

        socket_getpeername($newsock, $ip);
        echo "New client connected: {$ip}\n";

        // remove the listening socket from the clients-with-data array
        $key = array_search($sock, $read);
        unset($read[$key]);
    }

    // loop through all the clients that have data to read from
    foreach ($read as $read_sock) {
        // read until newline or 1024 bytes
        // socket_read while show errors when the client is disconnected, so silence the error messages
        $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);

        // check if the client is disconnected
        if ($data === false) {
            // remove client for $clients array
            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\n";
            // continue to the next client to read from, if any
            continue;
        }

        // trim off the trailing/beginning white spaces
        $data = trim($data);

        // check if there is any data after trimming off the spaces
        if (!empty($data)) {

            // send this to all the clients in the $clients array (except the first one, which is a listening socket)
            foreach ($clients as $send_sock) {

                // if its the listening sock or the client that we got the message from, go to the next one in the list
                if ($send_sock == $sock || $send_sock == $read_sock)
                    continue;

                // write the message to the client -- add a newline character to the end of the message
                socket_write($send_sock, $data . "\n");
            } // end of broadcast foreach
        }
    } // end of reading foreach
}

// close the listening socket
socket_close($sock);
?>

瀏覽器控制台向我顯示此錯誤:

WebSocket與“ ws://example.app:8080 /”的連接失敗:在收到握手響應之前,連接已關閉

WebSocket協議有很多功能,而不僅僅是原始套接字連接,這就是您的腳本試圖完成的工作。

有適用於PHP的websocket服務器庫。 我建議使用Ratchet 這個庫是一個很棒的實現。

如果您真的想自己實現WebSockets,則可能需要根據需要查看RFC 6455或其他標准之一。

另一方面,如果您正在尋找發布/訂閱和/或遠程過程調用功能,則可以簽出Thruway ,它是PHP的WAMP (Web應用程序消息協議)客戶端和服務器庫。

披露:我是Thruway的維護者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM