DLight\Application\WebSocket

Mô tả: WebSocket server đơn giản chạy trên CLI (sockets extension), xử lý handshake + parse frame RFC6455 và có event callback: onOpen, onMessage, onClose.

Public API

  • __construct(string $host = '0.0.0.0', int $port = 9501)
  • start(): void
  • send(\Socket $client, string $message): bool
  • public $onOpen, public $onMessage, public $onClose (callable)

Demo

use DLight\Application\WebSocket;

$ws = new WebSocket('0.0.0.0', 9501);

$ws->onOpen = function($client) {
  echo "open\n";
};

$ws->onMessage = function($client, $message) use ($ws) {
  // Echo back
  $ws->send($client, "You said: " . $message);
};

$ws->onClose = function($client) {
  echo "close\n";
};

$ws->start();

Chạy bằng CLI: php yourfile.php. Client (browser) có thể kết nối ws://host:9501.