Permalink
Browse files

Improve the example

  • Loading branch information...
1 parent febfed8 commit 2d1fb761047143f9ef652f33eb29d26820f15405 @mkoppanen committed Jan 25, 2012
Showing with 37 additions and 6 deletions.
  1. +37 −6 examples/non-blocking-client.php
@@ -7,19 +7,50 @@
/* Assign socket 1 to the queue, send and receive */
$retries = 5;
$sending = true;
+$sent = false;
+
+$queue->setSockOpt (ZMQ::SOCKOPT_LINGER, 1000);
+
+echo "Trying to send message\n";
do {
try {
if ($sending) {
- echo "Sending message\n";
- $sending = ($queue->send("This is a message", ZMQ::MODE_NOBLOCK) !== false);
- } else {
- echo "Got response: " . $queue->recv (ZMQ::MODE_NOBLOCK) . "\n";
- break;
+ if ($queue->send("This is a message", ZMQ::MODE_NOBLOCK) !== false) {
+ echo "Message sent\n";
+ $sent = true;
+ $sending = false;
+ }
+ }
+ } catch (ZMQSocketException $e) {
+ die(" - Error: " . $e->getMessage());
+ }
+ usleep (1000);
+} while (1 && --$retries);
+
+$retries = 2;
+$receiving = true;
+$received = false;
+
+echo "Trying to receive message\n";
+do {
+ try {
+ if ($receiving) {
+ $message = $queue->recv (ZMQ::MODE_NOBLOCK);
+
+ if ($message) {
+ echo "Received message: " . $message . "\n";
+ $receiving = false;
+ $received = true;
+ }
}
} catch (ZMQSocketException $e) {
die(" - Error: " . $e->getMessage());
}
- usleep(5);
+ sleep (1);
} while (1 && --$retries);
+if (!$received) {
+ echo "The receive timed out\n";
+}
+
?>

0 comments on commit 2d1fb76

Please sign in to comment.