1

私は Websocket サーバーを稼働させています。私自身の使用のために、静的 PHP スクリプトを実行したときに接続済みのリストを取得したいと考えています。

私のPHPスクリプトpull.php

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

$socket->send('data');

$test = $socket->recv();

var_dump($test);

次に、サーバースクリプトで:

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

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

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

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

$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();

そのため、サーバー スクリプトが実行されているときに、pull.php. responseこれは機能し、私が出力したテキストを送り返しますvar_dump。ただし、その後pull.phpスクリプトをリロードすると、ハングするだけです。理由はわかりませんが、どこかでデータを受信したことを相手に伝えていないため、別の要求を送信できないと想定しています。

また、現在、データをPusher クラスのメソッドに$pull->on('message', array($pusher, 'onPull'));送信しています。onPull()現時点では何も返しませんが、データを返す場合、これをリクエスタに戻すにはどうすればよいですか?

4

1 に答える 1

1

Pusherメソッドを確認できるように、クラスからコードを追加する必要があります...これは、ではなく、onPullほとんどがあなたのコードであるように見えます。の、はこちらpush/pullreq/reppush/pull

ただし、問題が発生していると思います(インラインのコメント):

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

$loop   = React\EventLoop\Factory::create();
/*******************
first red flag - you use your Pusher class, which presumably is set up
to handle a push/pull scenario rather than a req/rep scenario
*******************/
$pusher = new MyApp\Pusher;

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

/*******************
Why are you naming a REP socket $pull?  Change your code appropriately when you
copy/paste, it will help issues to stand out more
*******************/
$pull = $context->getSocket(ZMQ::SOCKET_REP);
$pull->bind('tcp://127.0.0.1:5552');
/*******************
second red flag - you set up a message handler that deals with a message event
and then you go on to manually call recv and send - this is probably your issue.
recv() will block until it receives your first request from the client, then it
will send your 'response', but this will only ever handle the very first message    
*******************/
$pull->on('message', array($pusher, 'onPull'));
$pull->recv();
$pull->send('response');

/*******************
I don't know what this is doing here, since your client is a ZMQ client and not an
HTTP client
*******************/
$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
);

/*******************
presumably this loop should handle subsequent messages, but it's either set up
for push/pull or HTTP, or both, I can't quite tell
*******************/
$loop->run();

ここでは、Ratchet や Websockets はまったく必要ないと思います。ZMQ クライアントと話したいだけですよね? だから、そのコードを引き出します。代わりに最も可能性が高いのは、on-messageハンドラーを定義してリクエストを処理し、レスポンスを送信することです。React次のことを試してください (これは、例を見つけることができなかった のごくわずかな調査に基づいているreq/repため、これが機能しない場合はお知らせください)

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

$loop   = React\EventLoop\Factory::create();
$context = new React\ZMQ\Context($loop);

$rep = $context->getSocket(ZMQ::SOCKET_REP);
$rep->bind('tcp://127.0.0.1:5552');
$rep->on('message', function($msg) use ($rep) {
    // the "use ($rep)" syntax is PHP 5.4 or later
    // if you're using an older version of PHP, just use $GLOBAL['rep'] instead
    $rep->send('response');
});

$loop->run();
于 2014-05-29T14:43:58.687 に答える