gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:14:22 25 4
gpt4 key购买 nike

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

$server->loop->addPeriodicTimer(1, function () use ($row, $server) {...

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

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

我用过 print_r($server)并已确认 clients对象在 _httpServer:protected 内元素。我想,如果我可以访问它,我就可以发送消息。

实际服务器的代码 video-server.php :
<?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 :
<?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();
}

}
?>

最佳答案

似乎您无法以您想要的方式获取该数据。
HttpServer 定义了一个 protected 变量。

protected $_httpServer; //<--- protected, you can't read from outside.
public function __construct(HttpServerInterface $component) {
$this->_httpServer = $component;
$this->_reqParser = new HttpRequestParser;
}

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

试一试:
$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
});

关于php - RatchetPHP 无法向循环中的所有客户端发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com