1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp; |
4
|
|
|
|
5
|
|
|
use EasyHttp\Contracts\WscCommonsContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* WebSocketConfig class |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/shahradelahi/easy-http |
11
|
|
|
* @author Arthur Kushman (https://github.com/arthurkushman) |
12
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
13
|
|
|
*/ |
14
|
|
|
class WebSocketConfig |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private string $scheme; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private string $host; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private string $user; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private string $password; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private string $port; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private array $headers = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var null|resource |
59
|
|
|
*/ |
60
|
|
|
private $context; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
private bool $hasProxy = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
private string $proxyIp; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
private string $proxyPort; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string|null |
79
|
|
|
*/ |
80
|
|
|
private ?string $proxyAuth; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
private array $contextOptions = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var int |
89
|
|
|
*/ |
90
|
|
|
private int $pingInterval = WscCommonsContract::DEFAULT_PING_INTERVAL; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
public function getTimeout(): int |
96
|
|
|
{ |
97
|
|
|
return $this->timeout; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $timeout |
102
|
|
|
* @return WebSocketConfig |
103
|
|
|
*/ |
104
|
|
|
public function setTimeout(int $timeout): WebSocketConfig |
105
|
|
|
{ |
106
|
|
|
$this->timeout = $timeout; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $int |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setPingInterval(int $int): WebSocketConfig |
115
|
|
|
{ |
116
|
|
|
$this->pingInterval = $int; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
public function getPingInterval(): int |
124
|
|
|
{ |
125
|
|
|
return $this->pingInterval; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function getHeaders(): array |
132
|
|
|
{ |
133
|
|
|
return $this->headers; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $headers |
138
|
|
|
* @return WebSocketConfig |
139
|
|
|
*/ |
140
|
|
|
public function setHeaders(array $headers): WebSocketConfig |
141
|
|
|
{ |
142
|
|
|
$this->headers = $headers; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
|
|
public function getFragmentSize(): int |
150
|
|
|
{ |
151
|
|
|
return $this->fragmentSize; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param int $fragmentSize |
156
|
|
|
* @return WebSocketConfig |
157
|
|
|
*/ |
158
|
|
|
public function setFragmentSize(int $fragmentSize): WebSocketConfig |
159
|
|
|
{ |
160
|
|
|
$this->fragmentSize = $fragmentSize; |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function getContext(): mixed |
168
|
|
|
{ |
169
|
|
|
return $this->context; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param mixed $context |
174
|
|
|
* @return WebSocketConfig |
175
|
|
|
*/ |
176
|
|
|
public function setContext(mixed $context): WebSocketConfig |
177
|
|
|
{ |
178
|
|
|
$this->context = $context; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
public function getScheme(): string |
186
|
|
|
{ |
187
|
|
|
return $this->scheme; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $scheme |
192
|
|
|
* @return WebSocketConfig |
193
|
|
|
*/ |
194
|
|
|
public function setScheme(string $scheme): WebSocketConfig |
195
|
|
|
{ |
196
|
|
|
$this->scheme = $scheme; |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function getHost(): string |
204
|
|
|
{ |
205
|
|
|
return $this->host; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $host |
210
|
|
|
* @return WebSocketConfig |
211
|
|
|
*/ |
212
|
|
|
public function setHost(string $host): WebSocketConfig |
213
|
|
|
{ |
214
|
|
|
$this->host = $host; |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function getUser(): string |
222
|
|
|
{ |
223
|
|
|
return $this->user; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param array $urlParts |
228
|
|
|
* @return WebSocketConfig |
229
|
|
|
*/ |
230
|
|
|
public function setUser(array $urlParts): WebSocketConfig |
231
|
|
|
{ |
232
|
|
|
$this->user = $urlParts['user'] ?? ''; |
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function getPassword(): string |
240
|
|
|
{ |
241
|
|
|
return $this->password; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param array $urlParts |
246
|
|
|
* @return WebSocketConfig |
247
|
|
|
*/ |
248
|
|
|
public function setPassword(array $urlParts): WebSocketConfig |
249
|
|
|
{ |
250
|
|
|
$this->password = $urlParts['pass'] ?? ''; |
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getPort(): string |
258
|
|
|
{ |
259
|
|
|
return $this->port; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param array $urlParts |
264
|
|
|
* @return WebSocketConfig |
265
|
|
|
*/ |
266
|
|
|
public function setPort(array $urlParts): WebSocketConfig |
267
|
|
|
{ |
268
|
|
|
$this->port = $urlParts['port'] ?? ($this->scheme === 'wss' ? '443' : '80'); |
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
public function getContextOptions(): array |
276
|
|
|
{ |
277
|
|
|
return $this->contextOptions; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param array $contextOptions |
282
|
|
|
* @return WebSocketConfig |
283
|
|
|
*/ |
284
|
|
|
public function setContextOptions(array $contextOptions): WebSocketConfig |
285
|
|
|
{ |
286
|
|
|
$this->contextOptions = $contextOptions; |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $ip |
292
|
|
|
* @param string $port |
293
|
|
|
* @return WebSocketConfig |
294
|
|
|
*/ |
295
|
|
|
public function setProxy(string $ip, string $port): WebSocketConfig |
296
|
|
|
{ |
297
|
|
|
$this->hasProxy = true; |
298
|
|
|
$this->proxyIp = $ip; |
299
|
|
|
$this->proxyPort = $port; |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Sets auth for proxy |
306
|
|
|
* |
307
|
|
|
* @param string $userName |
308
|
|
|
* @param string $password |
309
|
|
|
* @return WebSocketConfig |
310
|
|
|
*/ |
311
|
|
|
public function setProxyAuth(string $userName, string $password): WebSocketConfig |
312
|
|
|
{ |
313
|
|
|
$this->proxyAuth = (empty($userName) === false && empty($password) === false) ? base64_encode($userName . ':' . $password) : null; |
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return bool |
319
|
|
|
*/ |
320
|
|
|
public function hasProxy(): bool |
321
|
|
|
{ |
322
|
|
|
return $this->hasProxy; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return string|null |
327
|
|
|
*/ |
328
|
|
|
public function getProxyIp(): ?string |
329
|
|
|
{ |
330
|
|
|
return $this->proxyIp; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return string|null |
335
|
|
|
*/ |
336
|
|
|
public function getProxyPort(): ?string |
337
|
|
|
{ |
338
|
|
|
return $this->proxyPort; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return string|null |
343
|
|
|
*/ |
344
|
|
|
public function getProxyAuth(): ?string |
345
|
|
|
{ |
346
|
|
|
return $this->proxyAuth; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
} |
350
|
|
|
|