I'm developing a chat server by PHP Ratchet library and I have a doubt about sending message:
I have the two methods: onOpen(ConnectionInterface $conn)
and onError(ConnectionInterface $conn, \Exception $e)
.
When a client connects to chat onOpen method is called and I would to send a response message, for this reason I do $conn->send($data)
.
if there is an error onError
is called, but if there isn't error How do I know if everything has been successful since the flow continues anyway? (I do not have a success method).
I ask you the following question because I have to manage the following situation: when I send the message to a client, if the "send" method goes wrong I have to save on a database the message that I could not send and try to postpone it later, while if the sending was successful I do not have to save anything. My problem is that if the "send" method goes wrong, the "onError" method is called and I only have the instance about the "ConnectionInterface" but not the message I could not send, so at that point how could I? do to recover it?
I hope I was clear in explaining the problem
Maybe, some like this...
@example:
// ...
/**
* {@inheritDoc}
*/
public function onOpen(ConnectionInterface $connection)
{
// ...
$package = [
'data' => $data = [] // for example
];
$json = json_encode($package, JSON_FORCE_OBJECT);
$connection->currentMessage = $json;
$connection->send($json);
// $connection->send(null); // uncomment if you wanna simulate error (call onError())
}
// ...
/**
* {@inheritDoc}
*/
public function onError(ConnectionInterface $connection, \Exception $e)
{
$connection->currentMessage; <-- your current message
// ... save message, etc, ...whatever you want :)
$connection->close();
}
Have Fun!