1

I'm trying to get a request to my server via a websocket, and return a reply from the server. This is "sort of" working, however I can only do this once, any extra requests just hang somewhere.

Server Bind:

$pull = $context->getSocket(ZMQ::SOCKET_REP);
$pull->bind('tcp://127.0.0.1:5552');
$pull->on('message', array($pusher, 'onPull'));
$pull->recv();
$pull->send('back');

I have a static PHP file on my server, which when I run, want to return the reply from the server:

$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ, 'Sock');
$socket->connect("tcp://localhost:5552");

$socket->send('sending');

$message = $socket->recv();

echo "$message";

Now when I boot the server and run my php file, I get the "back" response back. However when I try to run it again it just hangs. I'm receiving the request each time?

Also, can anyone explain the $pull->on bit to me, I cannot find anywhere what it does.


Full server code:

<?php
    require './vendor/autoload.php';

    $loop   = React\EventLoop\Factory::create();
    $pusher = new MyApp\Pusher;

    $context = new React\ZMQ\Context($loop);

    $push = $context->getSocket(ZMQ::SOCKET_PULL);
    $push->bind('tcp://127.0.0.1:5555');
    $push->on('message', array($pusher, 'onNewPush'));

    $pull = $context->getSocket(ZMQ::SOCKET_REP);
    $pull->bind('tcp://127.0.0.1:5552');
    $pull->on('message', array($pusher, 'onPull'));
    $pull->recv();
    $pull->send('back');

    $webSock = new React\Socket\Server($loop);
    $webSock->listen(8080, '0.0.0.0');
    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                $pusher
            )
        ),
        $webSock
    );

    $loop->run();
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Alias
  • 2,983
  • 7
  • 39
  • 62
  • You need a loop, either explicitly (using while or for) or implicitly using react's message loop. You shouldn't use `bind()` and `on()` (which registers a message handler) in this loop! If none of this applies to your code, maybe you could enlarge it a bit so one could even run it for a test. – Ulrich Eckhardt May 26 '14 at 20:33
  • I added a link into the post for the full server code. So you're saying I need a loop in my static PHP file? – Alias May 26 '14 at 20:43
  • Oh, just found what's wrong: The `recv()` (and partially `send()`) shouldn't be there. The point is that when a message arrives, it calls `$pusher->onPull()` with the message and only there you call `$pull->send()` with the reply. Without that, you just call `$loop->run()` which is exactly the kind of loop I meant. – Ulrich Eckhardt May 26 '14 at 20:54
  • Oh I get you, how do I return that from my `onPull` method though? – Alias May 26 '14 at 21:25
  • Gah still can't work it out. – Alias May 27 '14 at 12:52

1 Answers1

2

I think something like this should do the job:

$pull->on(
    'message',
    function ($message) use ($pull) {
        $pull->send('response');
    }
);

In any case, whether you use an anonymous function like above or an object/method pair, you need access to $pull, because that is the communication channel that allows you to send messages. The example at http://socketo.me/docs/push, which seems to be the base of your code, doesn't need that, since it uses a pull socket, which only receives messages.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Thank you very much, it's working now. If I pass my `$pusher` variable into the `use()` as well, I'm able to access the class and send back the return value. Cheers :) – Alias May 27 '14 at 20:22