/** * Receive message from socket * Creates a new message and returns it * Blocks on recv if socket is not ready for input * * @throws Exception if no socket present * @return Zmsg */ public function recv() { if (!isset($this->_socket)) { throw new Exception("No socket supplied"); } $this->_parts = array(); while (true) { $this->_parts[] = $this->_socket->recv(); if (!$this->_socket->getSockOpt(ZMQ::SOCKOPT_RCVMORE)) { break; } } return $this; }
/** * Handle ZMQ Event. */ public function handleEvent() { while ($this->socket !== null) { $events = $this->socket->getSockOpt(ZMQ::SOCKOPT_EVENTS); $hasEvents = $events & ZMQ::POLL_IN || $events & ZMQ::POLL_OUT && $this->buffer->listening; if (!$hasEvents) { break; } if ($events & ZMQ::POLL_IN) { $this->handleReadEvent(); } if ($events & ZMQ::POLL_OUT && $this->buffer->listening) { $this->buffer->handleWriteEvent(); } } }
public function indexAction() { $context = new ZMQContext(); $socket = new ZMQSocket($context, ZMQ::SOCKET_DEALER); //$socket->setSockOpt(ZMQ::SOCKOPT_IDENTITY,"identity"); $socket->getSockOpt(ZMQ::SOCKOPT_IDENTITY); $socket->connect("tcp://127.0.0.1:5555"); $zmsg = new Zmq_Msg($socket); $zmsg->body_set("Hello world"); $zmsg->push("echo"); $zmsg->push("MDPC01"); $zmsg->push(""); printf("I: send request to 'echo' service: %s", PHP_EOL); echo $zmsg->__toString(); $zmsg->send(); return false; }
<?php /* * Weather proxy device * @author Ian Barber <ian(dot)barber(at)gmail(dot)com> */ $context = new ZMQContext(); // This is where the weather server sits $frontend = new ZMQSocket($context, ZMQ::SOCKET_SUB); $frontend->connect("tcp://192.168.55.210:5556"); // This is our public endpoint for subscribers $backend = new ZMQSocket($context, ZMQ::SOCKET_PUB); $backend->bind("tcp://10.1.1.0:8100"); // Subscribe on everything $frontend->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); // Shunt messages out to our own subscribers while (true) { while (true) { // Process all parts of the message $message = $frontend->recv(); $more = $frontend->getSockOpt(ZMQ::SOCKOPT_RCVMORE); $backend->send($message, $more ? ZMQ::MODE_SNDMORE : 0); if (!$more) { break; // Last message part } } }
public function getTimeout() { return $this->socket->getSockOpt(\ZMQ::SOCKOPT_RCVTIMEO) / 1000; }