Client.php
6.0 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace PHPSocketIO;
use PHPSocketIO\Parser\Parser;
class Client
{
public $server = null;
public $conn = null;
public $encoder = null;
public $decoder = null;
public $id = null;
public $request = null;
public $nsps = array();
public $connectBuffer = array();
public function __construct($server, $conn)
{
$this->server = $server;
$this->conn = $conn;
$this->encoder = new \PHPSocketIO\Parser\Encoder();
$this->decoder = new \PHPSocketIO\Parser\Decoder();
$this->id = $conn->id;
$this->request = $conn->request;
$this->setup();
Debug::debug('Client __construct');
}
public function __destruct()
{
Debug::debug('Client __destruct');
}
/**
* Sets up event listeners.
*
* @api private
*/
public function setup(){
$this->decoder->on('decoded', array($this,'ondecoded'));
$this->conn->on('data', array($this,'ondata'));
$this->conn->on('error', array($this, 'onerror'));
$this->conn->on('close' ,array($this, 'onclose'));
}
/**
* Connects a client to a namespace.
*
* @param {String} namespace name
* @api private
*/
public function connect($name){
if (!isset($this->server->nsps[$name]))
{
$this->packet(array('type'=> Parser::ERROR, 'nsp'=> $name, 'data'=> 'Invalid namespace'));
return;
}
$nsp = $this->server->of($name);
if ('/' !== $name && !isset($this->nsps['/']))
{
$this->connectBuffer[$name] = $name;
return;
}
$nsp->add($this, $nsp, array($this, 'nspAdd'));
}
public function nspAdd($socket, $nsp)
{
$this->sockets[$socket->id] = $socket;
$this->nsps[$nsp->name] = $socket;
if ('/' === $nsp->name && $this->connectBuffer)
{
foreach($this->connectBuffer as $name)
{
$this->connect($name);
}
$this->connectBuffer = array();
}
}
/**
* Disconnects from all namespaces and closes transport.
*
* @api private
*/
public function disconnect()
{
foreach($this->sockets as $socket)
{
$socket->disconnect();
}
$this->sockets = array();
$this->close();
}
/**
* Removes a socket. Called by each `Socket`.
*
* @api private
*/
public function remove($socket)
{
if(isset($this->sockets[$socket->id]))
{
$nsp = $this->sockets[$socket->id]->nsp->name;
unset($this->sockets[$socket->id]);
unset($this->nsps[$nsp]);
} else {
//echo('ignoring remove for '. $socket->id);
}
}
/**
* Closes the underlying connection.
*
* @api private
*/
public function close()
{
if (empty($this->conn)) return;
if('open' === $this->conn->readyState)
{
//echo('forcing transport close');
$this->conn->close();
$this->onclose('forced server close');
}
}
/**
* Writes a packet to the transport.
*
* @param {Object} packet object
* @param {Object} options
* @api private
*/
public function packet($packet, $preEncoded = false, $volatile = false)
{
if(!empty($this->conn) && 'open' === $this->conn->readyState)
{
if (!$preEncoded)
{
// not broadcasting, need to encode
$encodedPackets = $this->encoder->encode($packet);
$this->writeToEngine($encodedPackets, $volatile);
} else { // a broadcast pre-encodes a packet
$this->writeToEngine($packet);
}
} else {
// todo check
// echo('ignoring packet write ' . var_export($packet, true));
}
}
public function writeToEngine($encodedPackets, $volatile = false)
{
if($volatile)echo new \Exception('volatile');
if ($volatile && !$this->conn->transport->writable) return;
// todo check
if(isset($encodedPackets['nsp']))unset($encodedPackets['nsp']);
foreach($encodedPackets as $packet)
{
$this->conn->write($packet);
}
}
/**
* Called with incoming transport data.
*
* @api private
*/
public function ondata($data)
{
try {
// todo chek '2["chat message","2"]' . "\0" . ''
$this->decoder->add(trim($data));
} catch(\Exception $e) {
$this->onerror($e);
}
}
/**
* Called when parser fully decodes a packet.
*
* @api private
*/
public function ondecoded($packet)
{
if(Parser::CONNECT == $packet['type'])
{
$this->connect($packet['nsp']);
} else {
if(isset($this->nsps[$packet['nsp']]))
{
$this->nsps[$packet['nsp']]->onpacket($packet);
} else {
//echo('no socket for namespace ' . $packet['nsp']);
}
}
}
/**
* Handles an error.
*
* @param {Objcet} error object
* @api private
*/
public function onerror($err)
{
foreach($this->sockets as $socket)
{
$socket->onerror($err);
}
$this->onclose('client error');
}
/**
* Called upon transport close.
*
* @param {String} reason
* @api private
*/
public function onclose($reason)
{
if (empty($this->conn)) return;
// ignore a potential subsequent `close` event
$this->destroy();
// `nsps` and `sockets` are cleaned up seamlessly
foreach($this->sockets as $socket)
{
$socket->onclose($reason);
}
$this->sockets = null;
}
/**
* Cleans up event listeners.
*
* @api private
*/
public function destroy()
{
if (!$this->conn) return;
$this->conn->removeAllListeners();
$this->decoder->removeAllListeners();
$this->encoder->removeAllListeners();
$this->server = $this->conn = $this->encoder = $this->decoder = $this->request = $this->nsps = null;
}
}