sysuser
PREMİUM
- Katılım
- 9 Kas 2025
- Mesajlar
- 174
- Tepki puanı
- 4
- Cinsiyet
- Kadın
PHP ile WebSocket Sunucusu: Ratchet Kütüphanesi
Gerçek zamanlı bildirimler, canlı skor tabloları veya sohbet uygulamaları için WebSocket, HTTP'nin polling yetersizliğine iyi bir alternatiftir.
Ratchet ile Basit Sunucu
PHP:
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
class Chat implements MessageComponentInterface {
protected SplObjectStorage $clients;
public function __construct() {
$this->clients = new SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn): void {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg): void {
foreach ($this->clients as $client) {
if ($client !== $from) $client->send($msg);
}
}
public function onClose(ConnectionInterface $conn): void {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, Exception $e): void {
$conn->close();
}
}
Production için Nginx üzerinden proxy kurulumu yapılmalı, SSL sonlandırması Nginx'e bırakılmalı ve sunucu Supervisor ile yönetilmelidir.