<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Poll the items</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="zmqpoll.getlasterrors.html">ZMQPoll::getLastErrors</a></div> <div class="next" style="text-align: right; float: right;"><a href="zmqpoll.remove.html">ZMQPoll::remove</a></div> <div class="up"><a href="class.zmqpoll.html">ZMQPoll</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="zmqpoll.poll" class="refentry"> <div class="refnamediv"> <h1 class="refname">ZMQPoll::poll</h1> <p class="verinfo">(PECL zmq >= 0.5.0)</p><p class="refpurpose"><span class="refname">ZMQPoll::poll</span> — <span class="dc-title">Poll the items</span></p> </div> <div class="refsect1 description" id="refsect1-zmqpoll.poll-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">integer</span> <span class="methodname"><strong>ZMQPoll::poll</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$readable</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$writable</code></span> [, <span class="methodparam"><span class="type">integer</span> <code class="parameter">$timeout</code><span class="initializer"> = -1</span></span> ] )</div> <p class="para rdfs-comment"> Polls the items in the current poll set. The readable and writable items are returned in the <em><code class="parameter">readable</code></em> and <em><code class="parameter">writable</code></em> parameters. <span class="function"><a href="zmqpoll.getlasterrors.html" class="function">ZMQPoll::getLastErrors()</a></span> can be used to check if there were errors. </p> </div> <div class="refsect1 parameters" id="refsect1-zmqpoll.poll-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">readable</code></em></span> <dd> <p class="para"> Array where readable ZMQSockets/PHP streams are returned. The array will be cleared at the beginning of the operation. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">writable</code></em></span> <dd> <p class="para"> Array where writable ZMQSockets/PHP streams are returned. The array will be cleared at the beginning of the operation. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">timeout</code></em></span> <dd> <p class="para"> Timeout for the operation. -1 means that poll waits until at least one item has activity. Please note that starting from version 1.0.0 the poll timeout is defined in milliseconds, rather than microseconds. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 examples" id="refsect1-zmqpoll.poll-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4699"> <p><strong>Example #1 A <span class="function"><strong>ZMQPoll()</strong></span> example</strong></p> <div class="example-contents"><p> Create a simple poll server </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* Create socket, request-reply pattern (reply socket) */<br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= new </span><span style="color: #0000BB">ZMQContext</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$server </span><span style="color: #007700">= </span><span style="color: #0000BB">$context</span><span style="color: #007700">-></span><span style="color: #0000BB">getSocket</span><span style="color: #007700">(</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">SOCKET_REP</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Bind to port 5555 on 127.0.0.1 */<br /></span><span style="color: #0000BB">$server</span><span style="color: #007700">-></span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://127.0.0.1:5555"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Create new pollset for incoming/outgoing message */<br /></span><span style="color: #0000BB">$poll </span><span style="color: #007700">= new </span><span style="color: #0000BB">ZMQPoll</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Add the object and listen for poll in/out */<br /></span><span style="color: #0000BB">$id </span><span style="color: #007700">= </span><span style="color: #0000BB">$poll</span><span style="color: #007700">-></span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">, </span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">POLL_IN </span><span style="color: #007700">| </span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">POLL_OUT</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Added object with id " </span><span style="color: #007700">. </span><span style="color: #0000BB">$id </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Initialise readable and writable arrays */<br /></span><span style="color: #0000BB">$readable </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$writable </span><span style="color: #007700">= array();<br /><br />while (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* Amount of events retrieved */<br /> </span><span style="color: #0000BB">$events </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /> try {<br /> </span><span style="color: #FF8000">/* Poll until there is something to do */<br /> </span><span style="color: #0000BB">$events </span><span style="color: #007700">= </span><span style="color: #0000BB">$poll</span><span style="color: #007700">-></span><span style="color: #0000BB">poll</span><span style="color: #007700">(</span><span style="color: #0000BB">$readable</span><span style="color: #007700">, </span><span style="color: #0000BB">$writable</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$errors </span><span style="color: #007700">= </span><span style="color: #0000BB">$poll</span><span style="color: #007700">-></span><span style="color: #0000BB">getLastErrors</span><span style="color: #007700">();<br /><br /> if (</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$errors</span><span style="color: #007700">) > </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> foreach (</span><span style="color: #0000BB">$errors </span><span style="color: #007700">as </span><span style="color: #0000BB">$error</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Error polling object " </span><span style="color: #007700">. </span><span style="color: #0000BB">$error </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br /> }<br /> } catch (</span><span style="color: #0000BB">ZMQPollException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"poll failed: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br /><br /> if (</span><span style="color: #0000BB">$events </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* Loop through readable objects and recv messages */<br /> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$readable </span><span style="color: #007700">as </span><span style="color: #0000BB">$r</span><span style="color: #007700">) {<br /> try {<br /> echo </span><span style="color: #DD0000">"Received message: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$r</span><span style="color: #007700">-></span><span style="color: #0000BB">recv</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> } catch (</span><span style="color: #0000BB">ZMQException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"recv failed: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br /> }<br /><br /> </span><span style="color: #FF8000">/* Loop through writable and send back messages */<br /> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$writable </span><span style="color: #007700">as </span><span style="color: #0000BB">$w</span><span style="color: #007700">) {<br /> try {<br /> </span><span style="color: #0000BB">$w</span><span style="color: #007700">-></span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">"Got it!"</span><span style="color: #007700">);<br /> } catch (</span><span style="color: #0000BB">ZMQException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"send failed: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br /> }<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 returnvalues" id="refsect1-zmqpoll.poll-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an integer representing amount of items with activity. Throws ZMQPollException on error. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="zmqpoll.getlasterrors.html">ZMQPoll::getLastErrors</a></div> <div class="next" style="text-align: right; float: right;"><a href="zmqpoll.remove.html">ZMQPoll::remove</a></div> <div class="up"><a href="class.zmqpoll.html">ZMQPoll</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>