首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript无法连接到PHP Ratchet WebSocket服务器

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

Stack Overflow用户
提问于 2017-08-29 04:58:44
回答 1查看 2.1K关注 0票数 0

我一直在尝试在本地主机上配置Ratchet,并且一直在关注this tutorial

我已经安装了Composer和Ratchet,并准确地复制了该教程的PHP代码。当我运行服务器并使用telnet访问它时,我没有任何问题,它工作得很好。

但是,当我尝试使用JavaScript (使用HTML5 websockets)建立连接时,它不会连接-请求只是在一段时间后超时。我可以在PHP控制台和telnet中看到浏览器发送的初始HTTP请求消息,因此客户机显然可以很好地“连接”-只是看起来服务器没有确认这个请求。

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

然而,WebSocket +棘轮指南中没有提到需要实现这一点,所以我怀疑这可能不是问题所在。

我尝试了两个端口80808888,结果相同。我在Google Chrome60的macOS上使用XAMPP。

下面是我的JavaScript代码:

代码语言: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):

代码语言:javascript
复制
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;

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

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

$server->run();

下面是Chat类(src/MyApp/Chat.php):

代码语言:javascript
复制
<?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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-29 15:09:57

所以,你不需要实现握手,Ratchet为你做了这件事。

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

代码语言:javascript
复制
<?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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45927277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档