棘轮WAMP组件的问题

时间:2015-12-29 14:58:01

标签: php websocket ratchet

简要说明......

我正在尝试设置Ratchet Web Socket服务器并遇到一些问题。这是我想要实现的目标:

我的网站上有一个计数器,表示有多少用户注册了该网站。我希望这个计数器在其他用户注册网站时更新。 Simples ...

我尝试过什么

我只使用Ratchet大约24小时,所以我的经验至少可以说是极少,但尽管如此,我已经彻底阅读了文档,我相信自己走在了正确的轨道上。

这是我的代码:

推server.php

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

pusher.php

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));
来自JavaScript的

片段

// 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
    }
);

我的问题

以上所有工作,但在我进行全面测试和制作之前,我有几个问题:

  1. $persistent_id方法需要getSocket吗?我已阅读文档,但实际使用的是什么?我是否需要它,或者,我应该使用它吗?
  2. 我一直无法找到有关ZMQ \ Context类的on方法的文档,是否已弃用?我应该使用这个,还是应该使用recv
  3. 如何确保我的push-server.php一直在运行?是否有某种守护进程工具可用于确保它始终在运行并在服务器重新启动时自动启动?
  4. 是否可以将websocket附加到我的域而不是我的IP地址?当我在JavaScript中使用我的域时,我收到400错误...

0 个答案:

没有答案