Ratchet PHP zeromq回退到旧浏览器


Ratchet PHP zeromq fallback to older browsers

遵循此指令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
        }
    );
}
Ratchet支持web套接字js,这是一个自然的polyfill。ZMQ代码在服务器端,如果您的客户端使用本机WebSockets或Flash polyfill,它仍然会被执行。

保持Push教程中的代码不变,将web套接字js代码添加到客户端,然后查看FlashPolicy组件中的代码。

有关更多示例,请参阅此示例,了解如何在不必运行两个独立进程的情况下为Flash Policy文件提供服务器。

我相信我已经按照你的建议做了。这个想法是将聊天服务器与推送消息相结合。这一切都工作,除了闪光聚乙烯填充。这是我的服务器代码:

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