-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsocket_server.php
More file actions
107 lines (89 loc) · 3.7 KB
/
websocket_server.php
File metadata and controls
107 lines (89 loc) · 3.7 KB
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
<?php
require __DIR__ . "/vendor/autoload.php";
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
protected $users;
protected $ids;
public function __construct() {
//all the user connected to the web socket
$this->clients = new \SplObjectStorage;
//all the user that have "Online status"
$this->users = [];
//all users id
$this->ids = [];
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
//we handle two types of packets: status (for updating the status of users) and check(for checking if a user is online or not)
public function onMessage(ConnectionInterface $from, $msg) {
$data = json_decode($msg, true);
if ($data['type'] === 'status') {
//if the user is online, the array users is populated
if($data['status'] === 'online'){
$this->users[$data['userId']] = [$data['userId'], $data['latitude'], $data['longitude']];
$this->ids[$data['userId']] = $data['userId'];
}elseif($data['status'] === 'offline'){
//if the user is offline, the user is unest from the array
if(isset($this->users[$data['userId']]) && isset($this->ids[$data['userId']])){
unset($this->users[$data['userId']]);
unset($this->ids[$data['userId']]);
}
}
//after the user updating, we send the change of the user status over all the clients, that will be updated in real-time
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send(json_encode([
'type' => 'status',
'userId' => $data['userId'],
'status' => $data['status'],
'latitude' => $data['latitude'],
'longitude' => $data['longitude']
]));
$client->send(json_encode(['type' => 'total', 'number' => count($this->users), 'idArr' => $this->ids]));
}
}
var_dump($this->users);
}elseif($data['type'] === 'check') {
//if the user is in the users array, it means that the user is online; if not it means that the user is offline
if(isset($this->users[$data['userId']])){
$from->send(json_encode([
'type' => 'check',
'status' => 'online',
'latitude' => $this->users[$data['userId']][1],
'longitude' => $this->users[$data['userId']][2]
]));
}else{
$from->send(json_encode([
'type' => 'check',
'status' => 'offline'
]));
}
}elseif($data['type'] === 'total') {
$from->send(json_encode(['type' => 'total', 'number' => count($this->users), 'idArr' => $this->ids]));
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();