Workerman PHP
ตัวอย่างโค้ด PHP สำหรับ Workerman ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน
Workerman PHP
<?php
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Worker;
require __DIR__ . '/vendor/autoload.php';
$gif = base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function (TcpConnection $connection, Request $request) use ($gif) {
if ($request->path() !== '/pixel.gif') {
$connection->send(new Response(404));
return;
}
$connection->send(new Response(200, [
'Content-Type' => 'image/gif',
'Cache-Control' => 'no-store',
], $gif));
};
Worker::runAll();
เรารันตัวอย่างโค้ดนี้และเทียบไบต์ที่ได้กลับมาแล้ว
ตรวจสอบ
curl -sI http://localhost:8080/pixel.gif