Nsp.php
3.7 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace PHPSocketIO;
use PHPSocketIO\Event\Emitter;
use PHPSocketIO\Parser\Parser;
class Nsp extends Emitter
{
public $name = null;
public $server = null;
public $rooms = array();
public $flags = array();
public $sockets = array();
public $connected = array();
public $fns = array();
public $ids = 0;
public $acks = array();
public static $events = array(
'connect' => 'connect', // for symmetry with client
'connection' => 'connection',
'newListener' => 'newListener'
);
//public static $flags = array('json','volatile');
public function __construct($server, $name)
{
$this->name = $name;
$this->server = $server;
$this->initAdapter();
Debug::debug('Nsp __construct');
}
public function __destruct()
{
Debug::debug('Nsp __destruct');
}
public function initAdapter()
{
$adapter_name = $this->server->adapter();
$this->adapter = new $adapter_name($this);
}
public function to($name)
{
if(!isset($this->rooms[$name]))
{
$this->rooms[$name] = $name;
}
return $this;
}
public function in($name)
{
return $this->to($name);
}
public function add($client, $nsp, $fn)
{
$socket_name = $this->server->socket();
$socket = new $socket_name($this, $client);
if('open' === $client->conn->readyState)
{
$this->sockets[$socket->id]=$socket;
$socket->onconnect();
if(!empty($fn)) call_user_func($fn, $socket, $nsp);
$this->emit('connect', $socket);
$this->emit('connection', $socket);
}
else
{
echo('next called after client was closed - ignoring socket');
}
}
/**
* Removes a client. Called by each `Socket`.
*
* @api private
*/
public function remove($socket)
{
// todo $socket->id
unset($this->sockets[$socket->id]);
}
/**
* Emits to all clients.
*
* @return {Namespace} self
* @api public
*/
public function emit($ev = null)
{
$args = func_get_args();
if (isset(self::$events[$ev]))
{
call_user_func_array(array(__CLASS__, 'parent::emit'), $args);
}
else
{
// set up packet object
$parserType = Parser::EVENT; // default
//if (self::hasBin($args)) { $parserType = Parser::BINARY_EVENT; } // binary
$packet = array('type'=> $parserType, 'data'=> $args );
if (is_callable(end($args)))
{
echo('Callbacks are not supported when broadcasting');
return;
}
$this->adapter->broadcast($packet, array(
'rooms'=> $this->rooms,
'flags'=> $this->flags
));
$this->rooms = array();
$this->flags = array();;
}
return $this;
}
public function send()
{
$args = func_get_args();
array_unshift($args, 'message');
$this->emit($args);
return $this;
}
public function write()
{
$args = func_get_args();
return call_user_func_array(array($this, 'send'), $args);
}
public function clients($fn)
{
$this->adapter->clients($this->rooms, $fn);
return $this;
}
/**
* Sets the compress flag.
*
* @param {Boolean} if `true`, compresses the sending data
* @return {Socket} self
* @api public
*/
public function compress($compress)
{
$this->flags['compress'] = $compress;
return $this;
}
}