Php 棘轮WAMP组件的问题 简要说明。。。

Php 棘轮WAMP组件的问题 简要说明。。。,php,websocket,ratchet,Php,Websocket,Ratchet,我正在尝试设置Ratchet Web套接字服务器,但遇到了一些问题。以下是我努力实现的目标: 我的网站上有一个计数器,指示有多少用户注册了该网站。我想这个计数器更新,每当其他用户注册到该网站。简单的 我试过的 我只使用Ratchet大约24小时,所以我的经验非常有限,至少可以这么说,但是,我已经彻底阅读了文档,我相信我走上了正确的道路 这是我的密码: push-server.php pusher.php 来自注册脚本的代码段 JavaScript代码片段 我的问题 以上所有工作,但在我进行全面测

我正在尝试设置Ratchet Web套接字服务器,但遇到了一些问题。以下是我努力实现的目标:

我的网站上有一个计数器,指示有多少用户注册了该网站。我想这个计数器更新,每当其他用户注册到该网站。简单的

我试过的 我只使用Ratchet大约24小时,所以我的经验非常有限,至少可以这么说,但是,我已经彻底阅读了文档,我相信我走上了正确的道路

这是我的密码:

push-server.php pusher.php 来自注册脚本的代码段 JavaScript代码片段 我的问题 以上所有工作,但在我进行全面测试和生产之前,我有几个问题:

  • getSocket
    方法是否需要
    $persistent\u id
    ?我读过文档,但它实际上是用来做什么的?我需要它吗,或者,我应该使用它吗
  • 我找不到ZMQ\Context类的
    on
    方法的文档,这是否已被弃用?我应该使用这个还是应该使用
    recv
  • 如何确保我的
    push server.php
    一直在运行?我是否可以使用某种守护程序工具来确保它始终运行,并在服务器重新启动时自动启动
  • 是否可以将websocket附加到我的域而不是我的IP地址?当我在JavaScript中使用我的域时,我收到一个400错误
  • 对于3,请看一看。这将使您使用控制台命令作为守护进程运行。要保持守护进程运行,请查看。FWIW这最好分成4个问题。。
    // Autoload any required libraries
    require(dirname(__DIR__).'/vendor/autoload.php');
    
    // Initiate the loop and pusher
    $loop   = React\EventLoop\Factory::create();
    $pusher = new _sockets\pusher;
    
    // Listen for the web server to make a ZeroMQ push
    $context = new React\ZMQ\Context($loop);
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    
    // Binding to 127.0.0.1 means the only client that can connect is itself
    $pull->bind('tcp://127.0.0.1:5555');
    $pull->on('message', array($pusher,'message'));
    
    // Set up the web socket server for the clients
    $web_socket = new React\Socket\Server($loop);
    
    // Binding to 0.0.0.0 means remotes can connect
    $web_socket->listen(8080,'0.0.0.0');
    $web_server = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
            )
        ),
        $web_socket
    );
    
    // Run the loop
    $loop->run();
    
    namespace _sockets;
    use Ratchet\ConnectionInterface;
    use Ratchet\Wamp\WampServerInterface;
    
    class pusher implements WampServerInterface {
        /**
         * A lookup of all the topics clients have subscribed to
         */
        protected $subscribedTopics = array();
    
        public function onSubscribe(ConnectionInterface $conn, $topic) {
            $this->subscribedTopics[$topic->getId()] = $topic;
        }
        public function onUnSubscribe(ConnectionInterface $conn, $topic){
        }
        public function onOpen(ConnectionInterface $conn){
        }
        public function onClose(ConnectionInterface $conn){
        }
        public function onCall(ConnectionInterface $conn, $id, $topic, array $params){
        }
        public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible){
        }
        public function onError(ConnectionInterface $conn, \Exception $e){
        }
        public function message($data){
    
            $data = json_decode($data,true);
    
            // If the lookup topic object isn't set there is no one to publish to
            if(!array_key_exists($data['category'],$this->subscribedTopics)) {
                return;
            }
    
            $topic = $this->subscribedTopics[$data['category']];
    
            // re-send the data to all the clients subscribed to that category
            $topic->broadcast($data);
        }
    }
    
    // Push the sign up notice to all connections
    $context = new ZMQContext();
    $socket = $context->getSocket(ZMQ::SOCKET_PUSH);
    $socket->connect("tcp://localhost:5555");
    $array = array(
        'category' => 'user_signed_up'
    );
    $socket->send(json_encode($array));
    
    // Connect to the website
    var connection = new ab.Session('ws://MY_IP_ADDRESS:8080',
        function(){
            console.log('Connected to WebSocket');
            connection.subscribe('user_signed_up',function(topic,data){
                console.log(topic,data);
            });
        },
        function(){
            console.log('WebSocket Connection Closed');
        },
        {
            'skipSubprotocolCheck': true
        }
    );