forked from PocketDock/PocketDockConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
278 lines (237 loc) · 6.32 KB
/
Client.php
File metadata and controls
278 lines (237 loc) · 6.32 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace Wrench;
use Wrench\Payload\Payload;
use Wrench\Payload\PayloadHandler;
use Wrench\Util\Configurable;
use Wrench\Socket\ClientSocket;
use Wrench\Protocol\Protocol;
use \InvalidArgumentException;
use \RuntimeException;
/**
* Client class
*
* Represents a websocket client
*/
class Client extends Configurable
{
/**
* @var int bytes
*/
const MAX_HANDSHAKE_RESPONSE = '1500';
/**
* @var string
*/
protected $uri;
/**
* @var string
*/
protected $origin;
/**
* @var ClientSocket
*/
protected $socket;
/**
* Request headers
*
* @var array
*/
protected $headers = array();
/**
* Whether the client is connected
*
* @var boolean
*/
protected $connected = false;
/**
* @var PayloadHandler
*/
protected $payloadHandler = null;
/**
* Complete received payloads
*
* @var array<Payload>
*/
protected $received = array();
/**
* Constructor
*
* @param string $uri
* @param string $origin The origin to include in the handshake (required
* in later versions of the protocol)
* @param array $options (optional) Array of options
* - socket => Socket instance (otherwise created)
* - protocol => Protocol
*/
public function __construct($uri, $origin, array $options = array())
{
parent::__construct($options);
$uri = (string)$uri;
if (!$uri) {
throw new InvalidArgumentException('No URI specified');
}
$this->uri = $uri;
$origin = (string)$origin;
if (!$origin) {
throw new InvalidArgumentException('No origin specified');
}
$this->origin = $origin;
$this->protocol->validateUri($this->uri);
$this->protocol->validateOriginUri($this->origin);
$this->configureSocket();
$this->configurePayloadHandler();
}
/**
* Configure options
*
* @param array $options
* @return void
*/
protected function configure(array $options)
{
$options = array_merge(array(
'socket_class' => 'Wrench\\Socket\\ClientSocket',
'on_data_callback' => null
), $options);
parent::configure($options);
}
/**
* Configures the client socket
*/
protected function configureSocket()
{
$class = $this->options['socket_class'];
$this->socket = new $class($this->uri);
}
/**
* Configures the payload handler
*/
protected function configurePayloadHandler()
{
$this->payloadHandler = new PayloadHandler(array($this, 'onData'), $this->options);
}
/**
* Payload receiver
*
* Public because called from our PayloadHandler. Don't call us, we'll call
* you (via the on_data_callback option).
*
* @param Payload $payload
*/
public function onData(Payload $payload)
{
$this->received[] = $payload;
if (($callback = $this->options['on_data_callback'])) {
call_user_func($callback, $payload);
}
}
/**
* Adds a request header to be included in the initial handshake
*
* For example, to include a Cookie header
*
* @param string $name
* @param string $value
* @return void
*/
public function addRequestHeader($name, $value)
{
$this->headers[$name] = $value;
}
/**
* Sends data to the socket
*
* @param string $data
* @param int $type See Protocol::TYPE_*
* @param boolean $masked
* @return boolean Success
*/
public function sendData($data, $type = Protocol::TYPE_TEXT, $masked = true)
{
if (is_string($type) && isset(Protocol::$frameTypes[$type])) {
$type = Protocol::$frameTypes[$type];
}
$payload = $this->protocol->getPayload();
$payload->encode(
$data,
$type,
$masked
);
return $payload->sendToSocket($this->socket);
}
/**
* Receives data sent by the server
*
* @return array<Payload> Payload received since the last call to receive()
*/
public function receive()
{
if (!$this->isConnected()) {
return false;
}
$data = $this->socket->receive();
if (!$data) {
return $data;
}
$old = $this->received;
$this->payloadHandler->handle($data);
return array_diff_assoc($this->received, $old);
}
/**
* Connect to the server
*
* @return boolean Whether a new connection was made
*/
public function connect()
{
if ($this->isConnected()) {
return false;
}
$this->socket->connect();
$key = $this->protocol->generateKey();
$handshake = $this->protocol->getRequestHandshake(
$this->uri,
$key,
$this->origin,
$this->headers
);
$this->socket->send($handshake);
$response = $this->socket->receive(self::MAX_HANDSHAKE_RESPONSE);
return ($this->connected =
$this->protocol->validateResponseHandshake($response, $key));
}
/**
* Returns whether the client is currently connected
*
* Also checks the state of the underlying socket
*
* @return boolean
*/
public function isConnected()
{
if ($this->connected === false) {
return false;
}
// Check if the socket is still connected
if ($this->socket->isConnected() === false) {
$this->disconnect();
return false;
}
return true;
}
/**
* Disconnects the underlying socket, and marks the client as disconnected
*
* @param int $reason Reason for disconnecting. See Protocol::CLOSE_*
* @throws Exception\FrameException
* @throws Exception\SocketException
*/
public function disconnect($reason = Protocol::CLOSE_NORMAL)
{
$payload = $this->protocol->getClosePayload($reason);
if ($this->socket) {
$this->socket->send($payload->getPayload());
$this->socket->disconnect();
}
$this->connected = false;
}
}