-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime.php
108 lines (89 loc) · 3.42 KB
/
realtime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
// Gerçek zamanlı güncelleme için WebSocket bağlantısı kurma ve yönetme
// Composer ile vendor klasörünü dahil et
require __DIR__ . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Factory;
use React\Socket\SecureServer;
use React\Socket\Server;
/**
* WebSocket sunucusu için MessageComponentInterface uygulaması
*/
class RealTimeServer implements \Ratchet\MessageComponentInterface {
protected $clients;
protected $users = [];
public function __construct() {
$this->clients = new \SplObjectStorage;
echo "Gerçek zamanlı sunucu başlatıldı!\n";
}
public function onOpen(\Ratchet\ConnectionInterface $conn) {
// Yeni bir bağlantı oluşturulduğunda
$this->clients->attach($conn);
echo "Yeni bağlantı! ({$conn->resourceId})\n";
}
public function onMessage(\Ratchet\ConnectionInterface $from, $msg) {
// Bir istemciden mesaj alındığında
$data = json_decode($msg, true);
// Kullanıcı kimlik doğrulaması
if (isset($data['type']) && $data['type'] === 'auth') {
$this->users[$from->resourceId] = [
'user_id' => $data['user_id'],
'username' => $data['username']
];
echo "Kullanıcı {$data['username']} kimliği doğrulandı\n";
return;
}
// İşleme/Değişiklik bildirimi
if (isset($data['type']) && $data['type'] === 'update') {
// Mesajı diğer tüm bağlı kullanıcılara ilet
foreach ($this->clients as $client) {
if ($from !== $client) {
// Göndereni hariç tut ve mesajı diğer kullanıcılara ilet
$client->send(json_encode([
'type' => 'update',
'entity' => $data['entity'],
'action' => $data['action'],
'data' => $data['data'],
'user' => isset($this->users[$from->resourceId]) ? $this->users[$from->resourceId] : null
]));
}
}
}
}
public function onClose(\Ratchet\ConnectionInterface $conn) {
// Bağlantı kapatıldığında
$this->clients->detach($conn);
if (isset($this->users[$conn->resourceId])) {
echo "Kullanıcı {$this->users[$conn->resourceId]['username']} çıkış yaptı\n";
unset($this->users[$conn->resourceId]);
}
echo "Bağlantı {$conn->resourceId} kapatıldı\n";
}
public function onError(\Ratchet\ConnectionInterface $conn, \Exception $e) {
// Bir hata oluştuğunda
echo "Hata: {$e->getMessage()}\n";
$conn->close();
}
}
// WebSocket sunucusunu başlat
$loop = Factory::create();
$webSock = new Server('0.0.0.0:8080', $loop);
$server = new IoServer(
new HttpServer(
new WsServer(
new RealTimeServer()
)
),
$webSock,
$loop
);
echo "WebSocket sunucusu 8080 portunda çalışıyor...\n";
$server->run();
/*
Not: Bu dosyayı komut satırından çalıştırın:
php realtime.php
Ayrıca Composer ile aşağıdaki bağımlılıkları yüklemeniz gerekir:
composer require cboden/ratchet
*/