遵循这条指示http://socketo.me/docs/push我有这个工作。
但是,我希望我的服务兼容旧浏览器(IE 8等),所以我需要使用闪回后备(如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
}
);
}
答案 0 :(得分:4)
棘轮支持web-socket-js,这是一种天然的polyfill。 ZMQ代码位于服务器端,如果客户端使用本机WebSockets或Flash polyfill,则仍将执行。
保持您的代码不受Push教程的影响,将web-socket-js代码添加到您的客户端,然后查看FlashPolicy component中的代码。
有关更多参与示例,请参阅this example,了解如何在不必运行两个单独进程的情况下为Flash Policy文件提供服务。
答案 1 :(得分:0)
我相信我已经完成了你的建议。我们的想法是将聊天服务器与推送消息相结合。除闪光填料外,这一切都有效。这是我的服务器代码:
//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();