【发布时间】:2016-09-09 11:10:46
【问题描述】:
我正在使用 Ratchet Websocket 库在移动应用程序上聊天。
我使用了以下代码:发送/接收消息:
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct()
{
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
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);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
在 iOS 上,当用户进入聊天屏幕时,我调用 Connection Open 来打开移动设备和 Web 之间的连接。但是当我断开 Wifi 时。在服务器上 OnClose 没有被调用。所以服务器没有收到当前连接断开的通知。当服务器向应用程序发送任何消息时,iOS 应用程序不会收到它。我想知道有什么方法可以在OnMessage 函数中检测到$client->send($msg) 是否实际发送?
【问题讨论】: