首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RatchetPHP无法向循环中的所有客户端发送消息

RatchetPHP无法向循环中的所有客户端发送消息
EN

Stack Overflow用户
提问于 2018-07-17 00:47:30
回答 2查看 567关注 0票数 0

我正在使用Ratchet PHP向客户端发送消息,我正在使用

代码语言:javascript
运行
复制
$server->loop->addPeriodicTimer(1, function () use ($row, $server) {...

每秒发送一条消息。我可以echo消息和MySQL查询,但我无法实际访问$server中的clients对象,我可以访问$server->app,但是当我在这之后执行->clients时,它告诉我$clients不存在。

澄清一下,当我不使用new HttpServer(...)时,这不是问题,但是如果没有它,浏览器控制台就会说websocket握手是无效的,所以这不是一个好的解决办法。

我已经使用了print_r($server),并且已经确认clients对象位于_httpServer:protected项中。如果我能访问它,我想我就能发送消息了。

实际服务器video-server.php的代码

代码语言:javascript
运行
复制
<?php
include "../../include/db.info.php";

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Factory;
use MyApp\Chat;

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

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

$pdo = new PDO("mysql:host=localhost;port=3306;dbname=erewhon", "root", "");

$getUsername = $pdo->prepare("SELECT * FROM messages WHERE id=201");
$getUsername->execute();

$row = $getUsername->fetch(PDO::FETCH_ASSOC);

$server->loop->addPeriodicTimer(1, function () use ($row, $server) {        
    /*foreach ($server->app->component->clients as $client) {                  
            $client->send("hello client");          
    }*/
    print_r($server->app);
});

$server->run();
?>

类文件的代码chat.php

代码语言:javascript
运行
复制
<?php
namespace MyApp;
header("Content-Type: application/json; charset=UTF-8");


//include "../../db.info.php";


use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {


    public $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
        echo "Congratulations! the server is now running\n";
    }

    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) {
        //dont need this
    }

    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

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-17 01:47:56

看起来你不能以你想要的方式获取数据。HttpServer定义了一个受保护变量。

代码语言:javascript
运行
复制
protected $_httpServer; //<--- protected, you can't read from outside.
public function __construct(HttpServerInterface $component) {
    $this->_httpServer = $component;
    $this->_reqParser  = new HttpRequestParser;
}

但是,您可以传递一个Chat实例并跟踪它。它将指向相同的内存地址。

试一试:

代码语言:javascript
运行
复制
$chat = new Chat(); //<--- ADD THIS LINE
$server = IoServer::factory(
            new HttpServer(
              new WsServer(
                $chat //<----- USE HERE
              )
            ), 888
          );

....

$server->loop->addPeriodicTimer(1, function () use ($row, $server, $chat) {        
    /*foreach ($server->app->component->clients as $client) {                  
        $client->send("hello client");          
    }*/
    print_r($server->app);
    print_r($chat->clients); // <---- PRINT HERE TO GET THE INFO
});
票数 0
EN

Stack Overflow用户

发布于 2018-07-17 02:05:20

为了以防万一,我在下面得到了我的原始答案,但我想强调的是,被接受的答案是正确的方法,只是不要忘记将它传递给use

我知道我可能不应该这样做来解决我的问题,但它解决了问题,所以这就足够了:

我转到vendor\cboden\ratchet\src\Ratchet\Http并编辑了HttpServer.php,特别是变量protected $_httpServer,并将其更改为public $_httpServer,我可能不应该这样做,但这解决了我的问题。

我可以通过执行$server->app->_httpServer->component->clients来访问clients项。

感谢Felippe Duarte突出显示了这个属性,我没有想到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51366596

复制
相关文章

相似问题

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