DefaultAdapter.php
3.1 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
<?php
namespace PHPSocketIO;
class DefaultAdapter
{
public $nsp = null;
public $rooms = array();
public $sids = array();
public $encoder = null;
public function __construct($nsp)
{
$this->nsp = $nsp;
$this->encoder = new Parser\Encoder();
Debug::debug('DefaultAdapter __construct');
}
public function __destruct()
{
Debug::debug('DefaultAdapter __destruct');
}
public function add($id, $room)
{
$this->sids[$id][$room] = true;
$this->rooms[$room][$id] = true;
}
public function del($id, $room)
{
unset($this->sids[$id][$room]);
unset($this->rooms[$room][$id]);
if(empty($this->rooms[$room]))
{
unset($this->rooms[$room]);
}
}
public function delAll($id)
{
$rooms = array_keys(isset($this->sids[$id]) ? $this->sids[$id] : array());
foreach($rooms as $room)
{
$this->del($id, $room);
}
unset($this->sids[$id]);
}
public function broadcast($packet, $opts, $remote = false)
{
$rooms = isset($opts['rooms']) ? $opts['rooms'] : array();
$except = isset($opts['except']) ? $opts['except'] : array();
$flags = isset($opts['flags']) ? $opts['flags'] : array();
$packetOpts = array(
'preEncoded' => true,
'volatile' => isset($flags['volatile']) ? $flags['volatile'] : null,
'compress' => isset($flags['compress']) ? $flags['compress'] : null
);
$packet['nsp'] = $this->nsp->name;
$encodedPackets = $this->encoder->encode($packet);
if($rooms)
{
$ids = array();
foreach($rooms as $i=>$room)
{
if(!isset($this->rooms[$room]))
{
continue;
}
$room = $this->rooms[$room];
foreach($room as $id=>$item)
{
if(isset($ids[$id]) || isset($except[$id]))
{
continue;
}
if(isset($this->nsp->connected[$id]))
{
$ids[$id] = true;
$this->nsp->connected[$id]->packet($encodedPackets, $packetOpts);
}
}
}
} else {
foreach($this->sids as $id=>$sid)
{
if(isset($except[$id])) continue;
if(isset($this->nsp->connected[$id]))
{
$socket = $this->nsp->connected[$id];
$volatile = isset($flags['volatile']) ? $flags['volatile'] : null;
$socket->packet($encodedPackets, true, $volatile);
}
}
}
}
public function clients($rooms, $fn) {
$sids = array();
foreach ($rooms as $room) {
$sids = array_merge($sids, $this->rooms[$room]);
}
$fn();
}
}