我尝试构建一个小实时websocket用例,用户可以登录并查看所有其他登录用户,在新用户登录或现有用户注销。React/Ratchet/ZMQ中的多种订阅方法的最佳实践
对于这种情况,当用户登录或注销时,我在UserController中使用ZMQ PUSH套接字。
UserConstroller
public function login() {
//... here is the auth code, model call etc...
$aUserData = array();// user data comes from the database with username, logintime, etc....
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGIN_PUSHER'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
public function logout() {
//... here is the logout code, model call etc ....
$aUserData = array();// user data comes from the SESSION with username, logintime, etc....
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGOUT_PUSHER'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
然后我有一个推类像棘轮文档:link
在这个类有两种方法:onUserLogin和onUserLogout当然所有其他的东西,如
onSubscribe,的OnOpen,onPublish
UserInformationPusher
public function onUserLogin($aUserData) {
//var_dump("onUserLogin");
$sUserData = json_decode($aUserData, true);
$oTopic = $this->subscribedTopics["user_login"];
if($oTopic instanceof Topic) {
$oTopic->broadcast($sUserData);
} else {
return;
}
}
public function onUserLogout($aUserData) {
//var_dump("onUserLogout");
$entryData = json_decode($aUserData, true);
$oTopic = $this->subscribedTopics["user_logout"];
if($oTopic instanceof Topic) {
$oTopic->broadcast($entryData);
} else {
return;
}
}
的最后一部分是WampServer/WsServer/HttpServer的与一个循环,收听传入连接。还有我的ZMQ PULL插座
RatchetServerConsole
public function start_server() {
$oPusher = new UserInformationPusher();
$oLoop = \React\EventLoop\Factory::create();
$oZMQContext = new \React\ZMQ\Context($oLoop);
$oPullSocket = $oZMQContext->getSocket(\ZMQ::SOCKET_PULL);
$oPullSocket->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$oPullSocket->on('message', array($oPusher, 'onUserLogin'));
$oPullSocket->on('message', array($oPusher, 'onUserLogout'));
$oMemcache = new \Memcache();
$oMemcache->connect('127.0.0.1', 11211);
$oMemcacheHandler = new Handler\MemcacheSessionHandler($oMemcache);
$oSession = new SessionProvider(
new \Ratchet\Wamp\WampServer(
$oPusher
),
$oMemcacheHandler
);
//$this->Output->info("Server start initiation with memcache!...");
$webSock = new \React\Socket\Server($oLoop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$oServer = new \Ratchet\Server\IoServer(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
$oSession
)
),
$webSock
);
$this->Output->info("Server started ");
$oLoop->run();
}
在这个例子中,调用从登录()或退出()总是会调用两种方法(onUserLogin和onUserLogout)。 我无法找到一些文档,它描述了我可以在($ event,callable $ listener)方法上使用哪些事件,有没有人有链接/知识库? 什么是最好的方法来检查从UserController哪个方法被解雇?
- 我可以在控制器添加一些信息到$ sUserData,并在推检查这
- 我可以绑定的其他套接字连接到不同的端口(例如5554为推拉),并使用上( )方法
- 我可以...是否有另一个最佳实践来解决这个问题?
没有必要因为它的客户端代码工作正常
你能否提一下访问“new \ ZMQContext();”的步骤?因为我无法这样做。我已经问过这个问题在这里http://stackoverflow.com/questions/41054517/cakephp-3-react-zmq-library-namespace – i01000001 2016-12-09 07:00:42