php - Php Ratchet — 如何跟踪连接中断?
问题描述
如何在 Ratchet 中跟踪连接中断 - 我使用主页中的示例。但是当客户端断开连接时(也就是说,它并没有关闭它,而是例如当网络掉线和连接丢失时),onClose 或 onError 方法不会被调用......我如何跟踪这样的连接 -我想在它坏的时候做一些动作。
例子
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
// Make sure composer dependencies have been installed
require __DIR__ . '/vendor/autoload.php';
/**
* chat.php
* Send any incoming messages to all connected clients (except sender)
*/
class MyChat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client) {
if ($from != $client) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
// Run the server application through the WebSocket protocol on port 8080
$app = new Ratchet\App('localhost', 8080);
$app->route('/chat', new MyChat, array('*'));
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
$app->run();
如果客户端关闭了网络接口,连接会中断,但不会调用 onClose 或 onError 方法。如何检测和处理连接中断?
解决方案
推荐阅读
- python - 离开加入但保持一个观察
- ios - 如何删除 SwiftUI NavigationView 中的默认导航栏空间
- javascript - 如何合并同一数组中共享相同未知键的多个对象?
- javascript - 尝试将事件列表器添加到 js 文件
- php - 我应该使用全大写来执行 MySQL 命令以使其工作吗?
- javascript - 使用 React Native Alert 确认/阻止返回按钮导航
- python - 向循环函数添加分数
- php - 如何通过 PHP 自动更改 SQL 查询的 ID?
- javascript - 如何使用 Web-Worker 加速文件传输
- r - 编写一个函数来清理字符串数据并重命名列