【发布时间】:2017-06-25 01:18:18
【问题描述】:
所以,我正在运行一个 Ratchet (php) websocket 服务器,它有多个连接多个 Ratchet 应用程序 (MessageComponentInterfaces) 的路由:
//loop
$loop = \React\EventLoop\Factory::create();
//websocket app
$app = new Ratchet\App('ws://www.websocketserver.com', 8080, '0.0.0.0', $loop);
/*
* load routes
*/
$routeOne = '/example/route';
$routeOneApp = new RouteOneApp();
$app->route($routeOne, $routeOneApp, array('*'));
$routeTwo = '/another/route';
$routeTwoApp = new AnotherApp();
$app->route($routeTwo, $routeTwoApp, array('*'));
从这里我绑定一个 ZMQ 套接字,以便能够接收从在普通 apache 服务器上运行的 php 脚本发送的消息。
// 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:5050'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($routeOneApp, 'onServerMessage'));
终于启动服务器了:
//run
$loop->run();
只要我只将其中一个棘轮应用程序绑定到 ZMQ 套接字,它就可以正常工作。但是,我希望能够分别将消息推送到两个 Ratchet 应用程序。为此,我想将两个 ZMQ 套接字绑定到不同的路由,例如:
$pullOne->bind('tcp://127.0.0.1:5050' . $routeOne); // Binding to 127.0.0.1 means the only client that can connect is itself
$pullOne->on('message', array($routeOneApp, 'onServerMessage'));
和
$pullTwo->bind('tcp://127.0.0.1:5050' . $routeTwo); // Binding to 127.0.0.1 means the only client that can connect is itself
$pullTwo->on('message', array($routeTwoApp, 'onServerMessage'));
但是,当绑定第二个套接字时,这会导致来自 ZMQ 的错误消息,指出给定地址已在使用中。
所以问题是,有没有其他方法可以通过 ZMQ 套接字使用路由? 或者我应该使用其他方式来区分单独的 Ratchet 应用程序的消息,如果是这样,什么是一个好的解决方案? 我想过绑定到 2 个不同的端口,但认为这将是一个非常丑陋的解决方案?!
【问题讨论】:
-
您确定要在 PULL 中使用 bind() 吗?通常在 ZMQ 中,PUSH 端 binds(),PULL 端 connect()。
-
好问题,我从网上找到的一些教程中复制了绑定,它似乎有效。我会看看 connect() 是否也有效。
标签: php zeromq ratchet phpwebsocket