繁体   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