2017-01-04 58 views
1

ReachPHP服務器。代碼計數的請求數:如何在reactPHP服務器中保持狀態?

$counter = 0; 

$app = function ($request, $response) use ($counter){   
    $response->writeHead(200, array('Content-Type' => 'text/plain')); 
    $response->end($counter); 
    $counter++; 
}; 

$loop = React\EventLoop\Factory::create(); 
$socket = new React\Socket\Server($loop); 
$http = new React\Http\Server($socket, $loop); 

$http->on('request', $app); 
echo "Server running at http://127.0.0.1:1337\n"; 

$socket->listen(1337); 
$loop->run(); 

計數器保持爲0爲每一個請求,我怎樣才能使$ counter變量持續爲所有對服務器的請求?

+1

我沒有看到任何將改變'$ counter'。如果修改'$ counter'的代碼沒有包含在這裏,你可以在修改中發佈它嗎? – meun5

+0

是的,我錯過了$ counter ++; –

回答

0

只需要把& - 符號$櫃檯前參考轉移:

$counter = 0; 

$app = function ($request, $response) use (&$counter){   
    $response->writeHead(200, array('Content-Type' => 'text/plain')); 
    $response->end($counter); 
    $counter++; 
}; 

$loop = React\EventLoop\Factory::create(); 
$socket = new React\Socket\Server($loop); 
$http = new React\Http\Server($socket, $loop); 

$http->on('request', $app); 
echo "Server running at http://127.0.0.1:1337\n"; 

$socket->listen(1337); 
$loop->run(); 

我傻......

相關問題