簡體   English   中英

如何構建 Web Socket 連接?

[英]How to build a Web Socket Connection?

在我的 Android 項目中,我想實現 Web Sockets。 所以首先我嘗試集成服務器端 Websockets 並嘗試在命令行中對其進行測試。 以下鏈接顯示了我如何嘗試打開 Web 套接字連接。 我試圖訪問在 Web 套接字連接中創建的端口,它工作正常! 但是當我附加一條消息時,我從未收到它。 可能是因為沒有觸發 onMessage 函數?

https://i.stack.imgur.com/N8tsf.jpg

此外,我使用以下代碼打開 Websocket 連接並發送消息。

?php
ini_set('display_errors',1);
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require_once '/var/www/vendor/autoload.php';
class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}



    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ),
        8081
    );

    $server->run();
?>

如果我運行 php app.php 沒有任何錯誤。 那么實際上它應該可以發送和接收消息嗎? 此外,似乎沒有觸發 onMessage 函數。 但是如果我在公共函數 __construct() 函數中添加一個 echo 它會被打印出來,所以它可能是唯一被執行的函數。

我希望有人可以幫助我在這個 Web Socket Connection 中接收和打印消息。

謝謝你的幫助!

websocket 是一個socket,但是socket 不一定是websocket。 Telnet 不能很好地測試 websockets,因為它連接到套接字,但它不進行握手。

websocket 基本上是一個套接字,它需要在開始發送消息之前進行 http 握手。 這就是為什么看起來 Ratchet 程序沒有響應的原因。 它正在等待你方的握手完成。

為了讓它與您的服務器一起工作,您可能希望在 Android 端獲得一個 websocket 客戶端。 或者,您可以制作一個服務器,它只是一個普通的套接字服務器,而不是一個 websocket。

如果您正確完成了 websocket 握手,則在​​發送任何消息之前,您應該看到New connection! 在 php 端的終端中,因為你在 onOpen 函數中有什么。

如果您發送了類似以下內容的內容:

Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com

......它可能會起作用。

https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake

暫無
暫無

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

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