-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChat.php
More file actions
63 lines (56 loc) · 1.93 KB
/
Chat.php
File metadata and controls
63 lines (56 loc) · 1.93 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
<?php
/**
* Created by PhpStorm.
* User: ipetike
* Date: 2016.03.16.
* Time: 19:39
*/
class Chat implements \Ratchet\MessageComponentInterface {
protected $clients;
public function __construct(){
$this->clients = new SplObjectStorage();
}
/**
* When a new connection is opened it will be passed to this method
* @param \Ratchet\ConnectionInterface $conn The socket/connection that just connected to your application
* @throws \Exception
*/
function onOpen(\Ratchet\ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
/**
* This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed.
* @param \Ratchet\ConnectionInterface $conn The socket/connection that is closing/closed
* @throws \Exception
*/
function onClose(\Ratchet\ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
* the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
* @param \Ratchet\ConnectionInterface $conn
* @param \Exception $e
* @throws \Exception
*/
function onError(\Ratchet\ConnectionInterface $conn, \Exception $e)
{
$conn->close();
}
/**
* Triggered when a client sends data through the socket
* @param \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
* @param string $msg The message received
* @throws \Exception
*/
function onMessage(\Ratchet\ConnectionInterface $from, $msg)
{
foreach ( $this->clients as $client ){
if ( $client !== $from ){
$client->send($msg);
}
}
}
}