-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivecode_chat.php
More file actions
55 lines (41 loc) · 1.17 KB
/
livecode_chat.php
File metadata and controls
55 lines (41 loc) · 1.17 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
<?php
include("vendor/autoload.php");
// COLA :D
// Class < MessageComponentInterface: onOpen, onMessage, onClose; implement broadcast
// Server: IoServer::factory, HttpServer, WsServer, Class; $server->run
class ChatDemo implements \Ratchet\MessageComponentInterface {
public $clients;
public function __construct() {
$this->clients = new SplObjectStorage();
}
function onOpen(\Ratchet\ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "Connection opened... \n";
}
function onClose(\Ratchet\ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "Connection closed... \n";
}
function onError(\Ratchet\ConnectionInterface $conn, \Exception $e) {
echo "Error! {$e->getMessage()}\n";
$conn->close();
}
function onMessage(\Ratchet\ConnectionInterface $from, $msg) {
echo "Message: {$msg}\n";
$data = json_decode($msg);
foreach($this->clients as $client) {
$client->send(json_encode([
'nickname' => $data->nickname,
'message' => $data->message
]));
}
}
}
$server = \Ratchet\Server\IoServer::factory(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
new ChatDemo()
)
), 8080
);
$server->run();