PHP React \ ZMQ \ Context'ON'方法不会触发

时间:2018-06-14 10:23:44

标签: php websocket zeromq ratchet

我正在尝试使用php实现一个Web套接字服务器。

我已按照ratchet中的说明操作。但我无法将更改推送给客户。我知道问题是“React\ZMQ\Context”,它不会对任何事件做出反应。

正如我所料,此组件应对openclosemessageerror事件作出反应。这是我的代码:

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

// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context( $loop );
$pull    = $context->getSocket( ZMQ::SOCKET_PULL );
$pull->bind( 'tcp://127.0.0.1:9021' ); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on( 'open',    function( $msg ){ echo "open";    } );
$pull->on( 'close',   function( $msg ){ echo "close";   } );
$pull->on( 'message', function( $msg ){ echo "message"; } );
$pull->on( 'error',   function( $msg ){ echo "error";   } );

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server( '0.0.0.0:8090', $loop ); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
    new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new Ratchet\Wamp\WampServer(
                $pusher
            )
        )
    ),
    $webSock
);
$loop->run();

感谢。

1 个答案:

答案 0 :(得分:0)

这不起作用,除非您还修改了ZeroMQ基础架构设置的 PUSH - :

<?php
    // post.php ???
    // This all was here before  ;)
    $entryData = array(
        'category' => $_POST['category']
      , 'title'    => $_POST['title']
      , 'article'  => $_POST['article']
      , 'when'     => time()
    );

    $pdo->prepare( "INSERT INTO blogs (title, article, category, published) VALUES (?, ?, ?, ?)" )
        ->execute( $entryData['title'], $entryData['article'], $entryData['category'], $entryData['when'] );


// This is our new stuff
    $context = new ZMQContext();
    $socket  = $context->getSocket( ZMQ::SOCKET_PUSH, 'my pusher' );
    $socket->connect( "tcp://localhost:5555" );
//                                     ^^^^__________ MUST BECOME 9021 after your mod-s
    $socket->send( json_encode( $entryData ) );

如果这不起作用,请检查所有错误状态,根据记录的ZeroMQ本机API服务提供(实际代码取决于您的本机API语言包装/绑定)。

对于另一个级别的测试,可能有一组轻量级的测试来在python中设置一个简单的环境:

import     zmq
aClk     = zmq.Stopwatch()
aContext = zmq.Context()

aTarget  = "tcp://127.0.0.1:9021" # <- ADJUST TO PULL-er side transport-class endpoint

aPushToProveZMQ_RTO = aContext.Socket( zmq.PUSH )
aPushToProveZMQ_RTO.setsockopt(        zmq.LINGER, 0 )
aPushToProveZMQ_RTO.connect(           aTarget )

aClk.start()

try:
    print( "INF: infinite loop, sending messages [[[ Use Ctrl+C to break ]]]" )
    while True:
          aPushToProveZMQ_RTO.send( "aLoop RTT::({0:}[us])".format( aClk.stop() ) )
          pass;                                                     aClk.start()

except:
    pass

finally:
    aPushToProveZMQ_RTO.close()
    aContext.term()

    print( "INF: a gracefull exit" )