Subversion Repositories php-qbpwcf

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
namespace Ratchet\Wamp;
3
use Ratchet\ConnectionInterface;
4
 
5
/**
6
 * A topic/channel containing connections that have subscribed to it
7
 */
8
class Topic implements \IteratorAggregate, \Countable {
9
    private $id;
10
 
11
    private $subscribers;
12
 
13
    /**
14
     * @param string $topicId Unique ID for this object
15
     */
16
    public function __construct($topicId) {
17
        $this->id = $topicId;
18
        $this->subscribers = new \SplObjectStorage;
19
    }
20
 
21
    /**
22
     * @return string
23
     */
24
    public function getId() {
25
        return $this->id;
26
    }
27
 
28
    public function __toString() {
29
        return $this->getId();
30
    }
31
 
32
    /**
33
     * Send a message to all the connections in this topic
34
     * @param string|array $msg Payload to publish
35
     * @param array $exclude A list of session IDs the message should be excluded from (blacklist)
36
     * @param array $eligible A list of session Ids the message should be send to (whitelist)
37
     * @return Topic The same Topic object to chain
38
     */
39
    public function broadcast($msg, array $exclude = array(), array $eligible = array()) {
40
        $useEligible = (bool)count($eligible);
41
        foreach ($this->subscribers as $client) {
42
            if (in_array($client->WAMP->sessionId, $exclude)) {
43
                continue;
44
            }
45
 
46
            if ($useEligible && !in_array($client->WAMP->sessionId, $eligible)) {
47
                continue;
48
            }
49
 
50
            $client->event($this->id, $msg);
51
        }
52
 
53
        return $this;
54
    }
55
 
56
    /**
57
     * @param  WampConnection $conn
58
     * @return boolean
59
     */
60
    public function has(ConnectionInterface $conn) {
61
        return $this->subscribers->contains($conn);
62
    }
63
 
64
    /**
65
     * @param WampConnection $conn
66
     * @return Topic
67
     */
68
    public function add(ConnectionInterface $conn) {
69
        $this->subscribers->attach($conn);
70
 
71
        return $this;
72
    }
73
 
74
    /**
75
     * @param WampConnection $conn
76
     * @return Topic
77
     */
78
    public function remove(ConnectionInterface $conn) {
79
        if ($this->subscribers->contains($conn)) {
80
            $this->subscribers->detach($conn);
81
        }
82
 
83
        return $this;
84
    }
85
 
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getIterator() {
90
        return $this->subscribers;
91
    }
92
 
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function count() {
97
        return $this->subscribers->count();
98
    }
99
}