-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatTopic.php
59 lines (49 loc) · 1.53 KB
/
ChatTopic.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
<?php
namespace JDare\ClankChatBundle\Topic;
use Ratchet\ConnectionInterface as Conn;
use JDare\ClankBundle\Server\App\Handler\TopicHandlerInterface;
class ChatTopic implements TopicHandlerInterface
{
/**
* Announce to this topic that someone else has joined the chat room
*
* Also, set their nickname to Guest if it doesnt exist.
*
* @param \Ratchet\ConnectionInterface $conn
* @param $topic
*/
public function onSubscribe(Conn $conn, $topic)
{
if (!isset($conn->ChatNickname))
{
$conn->ChatNickname = "Guest";
}
$msg = $conn->ChatNickname . " joined the chat room.";
$topic->broadcast(array("msg" => $msg, "from" => "System"));
}
/**
* Announce person left chat room
*
* @param \Ratchet\ConnectionInterface $conn
* @param $topic
*/
public function onUnSubscribe(Conn $conn, $topic)
{
$msg = $conn->ChatNickname . " left the chat room.";
$topic->broadcast(array("msg" => $msg, "from" => "System"));
}
/**
* Do some nice things like strip html/xss etc. then pass along the message
*
* @param \Ratchet\ConnectionInterface $conn
* @param $topic
* @param $event
* @param array $exclude
* @param array $eligible
*/
public function onPublish(Conn $conn, $topic, $event, array $exclude, array $eligible)
{
$event = htmlentities($event); // removing html/js
$topic->broadcast(array("msg" => $event, "from" => $conn->ChatNickname));
}
}