/** * @param Client $client * * @return $this */ public function run(Client $client) { $client->init(); $context = $client->getContext(); $context->zmqContext = $this->getZmqService()->createContext(); $context->zmqOutgoingSockets = $this->getZmqService()->createSockets($client->getOutgoingSockets(), $context->zmqContext); $client->onMessageSent(function ($socketName, $msg, Client $client) { $sockets = $client->getContext()->zmqOutgoingSockets; if (!isset($sockets[$socketName])) { throw new \RuntimeException(sprintf("Unable to send message: unknown outgoing socket '%s'", $socketName), 500); } /** @var NamedSocket $socket */ $socket = $sockets[$socketName]; $socket->send(json_encode($msg)); $response = null; if ($socket->getSocketType() === \ZMQ::SOCKET_REQ) { $rawMsg = $socket->recv(); $response = @json_decode($rawMsg, true); if (!$response) { throw new \RuntimeException(sprintf("Unable to parse incoming message: %s", $rawMsg), 412); } } return $response; }); $client->execute(); return $this; }