【问题标题】:Ratchet PHP zeromq fallback to older browsers棘轮 PHP zeromq 回退到旧版浏览器
【发布时间】:2013-08-19 01:04:53
【问题描述】:

按照这条指令 http://socketo.me/docs/push 我已经完成了这项工作。

但是,我希望我的服务也与旧版浏览器(IE 8 等)兼容,因此我需要使用 Flash 后备(如 web_socket.js 或类似)来执行此操作:

function initPull() {
    var conn = new ab.Session(

        // The host (our Ratchet WebSocket server) to
        'ws://'+PUSH_SOCKET_SERVER+':'+PUSH_SOCKET_PORT+'/',

        // Once the connection has been established
        function() {            
            conn.subscribe(TOPIC_PREFIX+rID, function(topic, data) {

                //Trigger action
                aTrigger(data);
            });
        },

        // When the connection is closed
        function() {            
            console.warn('WebSocket connection closed');
        },

        // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
        {
            'skipSubprotocolCheck': true
        }
    );
}

【问题讨论】:

    标签: php compatibility zeromq fallback ratchet


    【解决方案1】:

    Ratchet 支持web-socket-js,这是一种天然的 polyfill。 ZMQ 代码位于服务器端,如果您的客户端使用本机 WebSockets 或 Flash polyfill,仍将执行该代码。

    保持推送教程中的代码不变,将 web-socket-js 代码添加到客户端,然后查看来自 FlashPolicy component 的代码。

    有关更复杂的示例,请参阅this example,了解如何在不运行两个单独进程的情况下提供 Flash 策略文件。

    【讨论】:

      【解决方案2】:

      我相信我已经按照您的建议做了。这个想法是将聊天服务器与推送消息结合起来。这一切都有效,除了 flash polyfill。这是我的服务器代码:

      //This is a server to handle both WAMP chat messages and PUSH messages
      use Ratchet\Server\IoServer;
      use Ratchet\Server\FlashPolicy;
      use Ratchet\WebSocket\WsServer;
      use Ratchet\Wamp\ServerProtocol;
      
      use React\EventLoop\Factory;
      use React\Socket\Server as Reactor;
      use React\ZMQ\Context;
      
      use Ratchet\Wamp\WampServer;
      use Ratchet\Cookbook\OpenPubSub;
      use Ratchet\Website\PortLogger;
      use Ratchet\Cookbook\NullComponent;
      use Ratchet\Cookbook\MessageLogger;
      use Ratchet\Push\Pusher;
      
      
      use Monolog\Logger;
      use Monolog\Handler\StreamHandler;
      
      
      // Composer: The greatest thing since sliced bread
      require dirname(__DIR__) . '/vendor/autoload.php';
      
      // Setup logging
      $stdout = new StreamHandler('php://stdout');
      $logout = new Logger('SockOut');
      $login  = new Logger('Sock-In');
      $login->pushHandler($stdout);
      $logout->pushHandler($stdout);
      
      // The all mighty event loop
      $loop = Factory::create();
      
      // This little thing is to check for connectivity...
      // As a case study, when people connect on port 80, we're having them
      //  also connect on port 9000 and increment a counter if they connect.
      // Later, we can publish the results and find out if WebSockets over 
      //  a port other than 80 is viable (theory is blocked by firewalls).
      $context = new Context($loop);
      $push = $context->getSocket(ZMQ::SOCKET_PUSH);
      $push->connect('tcp://127.0.0.1:8080');
      
      // Setup our Ratchet ChatRoom application
      $webSock = new Reactor($loop);
      $webSock->listen(8080, '0.0.0.0');
      $webServer = new IoServer(           // Basic I/O with clients, aww yeah
          new WsServer(                    // Boom! WebSockets
              new PortLogger($push, 80,    // Compare vs the almost over 9000 conns
                  new MessageLogger(       // Log events in case of "oh noes"
                    new WampServer(
                      new OpenPubSub
                    )
                    , $login
                    , $logout
                  )
              )
          )
        , $webSock
      );
      
      // Allow Flash sockets (Internet Explorer) to connect to our app
      $flashSock = new Reactor($loop);
      $flashSock->listen(843, '0.0.0.0');
      $policy = new FlashPolicy;
      $policy->addAllowedAccess('*', 80);
      $policy->addAllowedAccess('*', 8080);
      $policy->addAllowedAccess('*', 8081);
      $webServer = new IoServer($policy, $flashSock);
      
      $logSock = new Reactor($loop);
      $logSock->listen(9000, '0.0.0.0');
      $zLogger = new IoServer(
          new WsServer(
            new MessageLogger(
              new PortLogger($push, 9000, new NullComponent)
              )
          )
        , $logSock
      );
      
      
      $pusher = new 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:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
      $pull->on('message', array($pusher, 'onMsg'));
      
      // Set up our WebSocket server for clients wanting real-time updates
      $webSock = new React\Socket\Server($loop);
      $webSock->listen(8081, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
      $webServer = new Ratchet\Server\IoServer(
          new Ratchet\WebSocket\WsServer(
              new MessageLogger(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
              )
          ),
          $webSock
      );    
      
      // GO GO GO!
      echo "[".Date("Y-m-d H:i:s")."] VPCserver started...\n";
      $loop->run();
      

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 1970-01-01
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多