Javascript 无法连接到 PHP Ratchet WebSocket 服务器

Javascript can't connect to PHP Ratchet WebSocket server

我一直在尝试配置 Ratchet on localhost, and have been following this tutorial

我已经安装了 Composer 和 Ratchet,并且完全复制了该教程中的 PHP 代码。当我 运行 服务器并使用 telnet 访问它时,我没有问题,它工作正常。

但是,当我尝试使用 JavaScript 建立连接(使用 HTML5 websockets)时,它没有连接 - 请求只是在一段时间后超时。我可以在 PHP 控制台和 telnet 中看到我的浏览器发送的初始 HTTP 请求消息,因此客户端显然可以 "connect" 正常 - 似乎服务器没有确认此请求.

我事先在 Whosebug 和其他类似网站上调查了其他人的问题,有些人提到服务器必须发回 HTTP 回复,我试过这样做(使用 send 方法对于最近连接的客户端,如果他们的消息以 GET HTTP/1.1 开头)。我在 MDN 上查找了一些关于此的规范,发现 this guide,但我的实现对问题没有影响 - JavaScript 仍然无法连接。我不确定这是否是因为我错误地实现了握手代码,或者它是否根本不是我最初问题的解决方案。

WebSocket + Ratchet 指南的

None 提到需要实现这一点,所以我怀疑这可能不是问题所在。

我已经尝试了 80808888 这两个端口,结果都是一样的。我在 Google Chrome 60.

的 macOS 上使用 XAMPP

这是我的 JavaScript 代码:

window.onload = function() {
    var conn = new WebSocket('ws://localhost:8080');
    conn.onmessage = function(e) {
        console.log(e.data);
    }
    conn.onopen = function(e) {
        console.log("Connection established!");
    }
}

这是我的 PHP 服务器代码 (bin/chat-server.php):

<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

require dirname(__DIR__) . '/vendor/autoload.php';

$server = IoServer::factory(
    new Chat(),
    8080
);

$server->run();

这里是聊天 class (src/MyApp/Chat.php):

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

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();
    }
}

因此,您不需要实施握手,Ratchet 会为您完成。

要让代码像我上面那样工作,你需要做的就是确保你让它像这样使用 WebServer:

<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;

require dirname(__DIR__) . '/vendor/autoload.php';

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

$server->run();