mkoppanen/php-zmq — Issue #45: problem with zmq work with libevent  in php
Author: yangzhu6263736
Created: 2011-11-23 11:04:35+00:00
State: open

//myserver
function print_line($fd, $events, $arg) {
static $msgs = 1;
echo "CALLBACK FIRED" . PHP_EOL;
echo $arg[0]->recv();
if($arg[0]->getsockopt (ZMQ::SOCKOPT_EVENTS) & ZMQ::POLL_IN) {
echo "Got incoming data" . PHP_EOL;
var_dump ($arg[0]->recv());
}
}
$base = event_base_new();
$event = event_new();
$context = new ZMQContext();
$rep = $context->getSocket(ZMQ::SOCKET_PULL);
$rep->bind("tcp://192.168.2.112:5555");
$fd = $rep->getsockopt(ZMQ::SOCKOPT_FD);
event_set($event, $fd, EV_READ | EV_PERSIST, "print_line", array($rep, $base));
event_base_set($event, $base);
event_add($event);
event_base_loop($base);

//client
for($i=0;$i<10;$i++) {
testSend($i);
}

function testSend($i)
{
if (!$queue) {
$queue = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_PUSH, "MySock1");
$queue->connect("tcp://192.168.2.112:5555");
}
$msg = "testword{$i}\r\n";
$queue->send($msg);
echo "done{$i}\r\n";
}

problem: i can only receive the first msg; but when i use zmq only on my server it works ok;
like this

$context = new ZMQContext();
$responder = new ZMQSocket($context, ZMQ::SOCKET_PULL);
$responder->bind('tcp://192.168.2.112:5555');
try{
while ($request = $responder->recv()) {
echo 'Message received...'.$request . "\n";
}
} catch (ZMQSocketException $e) {
print_r($e);
}

How I want to solve this problem？