gpt4 book ai didi

php - RatchetPHP - 客户端在收到来自服务器的消息之前与服务器断开连接

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

所以,我正在尝试 Ratchet 教程( http://socketo.me/docs/hello-world ),但我遇到了一个问题:
每次在 ConnectionInterface 对象上调用 send() 方法时,该客户端都会与服务器断开连接。

下面的例子:

SSH终端

php server1.php
New connection! (54)
New connection! (83)
Connection 83 sending message "test msg" to 1 other connection
Connection 54 has disconnected

我什至没有在客户端 54 上收到“测试消息”。

我的文件:

server1.php

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

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

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

$server->run();

Chat1.php

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

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

最佳答案

我有这样的错误。
当我检查错误时,我注意到在客户端,当我收到消息时,我在接收方法中写了一些会导致连接从客户端断开的东西。
此错误与服务器无关

关于php - RatchetPHP - 客户端在收到来自服务器的消息之前与服务器断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794781/

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