enableKeepAlive配置似乎不起作用,文档甚至没有解释如何正确设置它。 http://socketo.me/docs/websocket
我通过手机打开连接然后关闭wifi和数据来测试它。客户端从未断开连接。
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\OriginCheck;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$loop->run();
$checkedApp = new WsServer( new Chat() );
$checkedApp->enableKeepAlive($loop, 5);
//$checkedApp = new OriginCheck(new WsServer( new Chat() ), array('localhost'));
$checkedApp = new OriginCheck($checkedApp);
$checkedApp->allowedOrigins[] = 'mywebsite.com';
$server = IoServer::factory(
new HttpServer(
$checkedApp
),
8082
);
//works, so loop works
/*
$server->loop->addPeriodicTimer(5, function () use ($server) {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
*/
$server->run();
答案 0 :(得分:0)
太棒了,所以在本页面上有所介绍: http://socketo.me/docs/migration-3
创建服务器后,然后启用KeepAlive:
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\OriginCheck;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
//$loop = React\EventLoop\Factory::create();
//$loop->run();
$checkedApp = new WsServer( new Chat() );
//$checkedApp->enableKeepAlive($loop, 5);
//$checkedApp = new OriginCheck(new WsServer( new Chat() ), array('localhost'));
$checkedAppToPass = new OriginCheck($checkedApp);
$checkedAppToPass->allowedOrigins[] = 'mywebsite.com';
$server = IoServer::factory(
new HttpServer(
$checkedAppToPass
),
8082
);
$checkedApp->enableKeepAlive($server->loop, 10);
//works, so loop works
/*
$server->loop->addPeriodicTimer(5, function () use ($server) {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
*/
$server->run();