@@ -8,140 +8,45 @@ This library contains WebSocket client and server for PHP.
8
8
The client and server provides methods for reading and writing to WebSocket streams.
9
9
It does not include convenience operations such as listeners and implicit error handling.
10
10
11
+ ## Documentation
12
+
13
+ - [ Client] ( docs/Server.md )
14
+ - [ Server] ( docs/Server.md )
15
+ - [ Changelog] ( docs/Changelog.md )
16
+ - [ Contributing] ( docs/Contributing.md )
17
+ - [ License] ( docs/COPYING.md )
18
+
11
19
## Installing
12
20
13
21
Preferred way to install is with [ Composer] ( https://getcomposer.org/ ) .
14
22
```
15
23
composer require textalk/websocket
16
24
```
17
25
18
- Current version support PHP versions ` ^7.1 ` .
19
- For PHP ` ^5.4 ` and ` ^7.0 ` support use version ` 1.3 ` .
26
+ * Current version support PHP versions ` ^7.2 ` .
27
+ * For PHP ` 7.1 ` support use version ` 1.4 ` .
28
+ * For PHP ` ^5.4 ` and ` 7.0 ` support use version ` 1.3 ` .
20
29
21
30
## Client
22
31
23
- The client can read and write on a WebSocket stream.
32
+ The [ client] ( docs/Server.md ) can read and write on a WebSocket stream.
24
33
It internally supports Upgrade handshake and implicit close and ping/pong operations.
25
34
26
- ### Class synopsis
27
-
28
- ``` php
29
- WebSocket\Client {
30
-
31
- public __construct(string $uri, array $options = [])
32
- public __destruct()
33
-
34
- public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void
35
- public receive() : mixed
36
- public close(int $status = 1000, mixed $message = 'ttfn') : mixed
37
-
38
- public getLastOpcode() : string
39
- public getCloseStatus() : int
40
- public isConnected() : bool
41
- public setTimeout(int $seconds) : void
42
- public setFragmentSize(int $fragment_size) : self
43
- public getFragmentSize() : int
44
- public setLogger(Psr\Log\LoggerInterface $logger = null) : void
45
- }
46
- ```
47
-
48
- ### Example: Simple send-receive operation
49
-
50
- This example send a single message to a server, and output the response.
51
-
52
35
``` php
53
36
$client = new WebSocket\Client("ws://echo.websocket.org/");
54
37
$client->send("Hello WebSocket.org!");
55
38
echo $client->receive();
56
39
$client->close();
57
40
```
58
41
59
- ### Example: Listening to a server
60
-
61
- To continuously listen to incoming messages, you need to put the receive operation within a loop.
62
- Note that these functions ** always** throw exception on any failure, including recoverable failures such as connection time out.
63
- By consuming exceptions, the code will re-connect the socket in next loop iteration.
64
-
65
- ``` php
66
- $client = new WebSocket\Client("ws://echo.websocket.org/");
67
- while (true) {
68
- try {
69
- $message = $client->receive();
70
- // Act on received message
71
- // Break while loop to stop listening
72
- } catch (\WebSocket\ConnectionException $e) {
73
- // Possibly log errors
74
- }
75
- }
76
- $client->close();
77
- ```
78
-
79
- ### Constructor options
80
-
81
- The ` $options ` parameter in constructor accepts an associative array of options.
82
-
83
- * ` timeout ` - Time out in seconds. Default 5 seconds.
84
- * ` fragment_size ` - Maximum payload size. Default 4096 chars.
85
- * ` context ` - A stream context created using [ stream_context_create] ( https://www.php.net/manual/en/function.stream-context-create ) .
86
- * ` headers ` - Additional headers as associative array name => content.
87
- * ` logger ` - A [ PSR-3] ( https://www.php-fig.org/psr/psr-3/ ) compatible logger.
88
- * ` persistent ` - Connection is re-used between requests until time out is reached. Default false.
89
-
90
- ``` php
91
- $context = stream_context_create();
92
- stream_context_set_option($context, 'ssl', 'verify_peer', false);
93
- stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
94
-
95
- $client = new WebSocket\Client("ws://echo.websocket.org/", [
96
- 'timeout' => 60, // 1 minute time out
97
- 'context' => $context,
98
- 'headers' => [
99
- 'Sec-WebSocket-Protocol' => 'soap',
100
- 'origin' => 'localhost',
101
- ],
102
- ]);
103
- ```
104
-
105
42
## Server
106
43
107
- The library contains a rudimentary single stream/single thread server.
44
+ The library contains a rudimentary single stream/single thread [ server] ( docs/Server.md ) .
108
45
It internally supports Upgrade handshake and implicit close and ping/pong operations.
109
46
110
47
Note that it does ** not** support threading or automatic association ot continuous client requests.
111
48
If you require this kind of server behavior, you need to build it on top of provided server implementation.
112
49
113
- ### Class synopsis
114
-
115
- ``` php
116
- WebSocket\Server {
117
-
118
- public __construct(array $options = [])
119
- public __destruct()
120
-
121
- public accept() : bool
122
- public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void
123
- public receive() : mixed
124
- public close(int $status = 1000, mixed $message = 'ttfn') : mixed
125
-
126
- public getPort() : int
127
- public getPath() : string
128
- public getRequest() : array
129
- public getHeader(string $header_name) : string|null
130
-
131
- public getLastOpcode() : string
132
- public getCloseStatus() : int
133
- public isConnected() : bool
134
- public setTimeout(int $seconds) : void
135
- public setFragmentSize(int $fragment_size) : self
136
- public getFragmentSize() : int
137
- public setLogger(Psr\Log\LoggerInterface $logger = null) : void
138
- }
139
- ```
140
-
141
- ### Example: Simple receive-send operation
142
-
143
- This example reads a single message from a client, and respond with the same message.
144
-
145
50
``` php
146
51
$server = new WebSocket\Server();
147
52
$server->accept();
@@ -150,160 +55,9 @@ $server->send($message);
150
55
$server->close();
151
56
```
152
57
153
- ### Example: Listening to clients
154
-
155
- To continuously listen to incoming messages, you need to put the receive operation within a loop.
156
- Note that these functions ** always** throw exception on any failure, including recoverable failures such as connection time out.
157
- By consuming exceptions, the code will re-connect the socket in next loop iteration.
158
-
159
- ``` php
160
- $server = new WebSocket\Server();
161
- while ($server->accept()) {
162
- try {
163
- $message = $server->receive();
164
- // Act on received message
165
- // Break while loop to stop listening
166
- } catch (\WebSocket\ConnectionException $e) {
167
- // Possibly log errors
168
- }
169
- }
170
- $server->close();
171
- ```
172
-
173
- ### Constructor options
174
-
175
- The ` $options ` parameter in constructor accepts an associative array of options.
176
-
177
- * ` timeout ` - Time out in seconds. Default 5 seconds.
178
- * ` port ` - The server port to listen to. Default 8000.
179
- * ` fragment_size ` - Maximum payload size. Default 4096 chars.
180
- * ` logger ` - A [ PSR-3] ( https://www.php-fig.org/psr/psr-3/ ) compatible logger.
181
-
182
- ``` php
183
- $server = new WebSocket\Server([
184
- 'timeout' => 60, // 1 minute time out
185
- 'port' => 9000,
186
- ]);
187
- ```
188
-
189
- ## Exceptions
190
-
191
- * ` WebSocket\BadOpcodeException ` - Thrown if provided opcode is invalid.
192
- * ` WebSocket\BadUriException ` - Thrown if provided URI is invalid.
193
- * ` WebSocket\ConnectionException ` - Thrown on any socket I/O failure.
194
- * ` WebSocket\TimeoutExeception ` - Thrown when the socket experiences a time out.
195
-
196
-
197
- ## Development and contribution
198
-
199
- Requirements on pull requests;
200
- * All tests ** MUST** pass.
201
- * Code coverage ** MUST** remain on 100%.
202
- * Code ** MUST** adhere to PSR-1 and PSR-12 code standards.
203
-
204
- Install or update dependencies using [ Composer] ( https://getcomposer.org/ ) .
205
- ```
206
- # Install dependencies
207
- make install
208
-
209
- # Update dependencies
210
- make update
211
- ```
212
-
213
- This project uses [ PSR-1] ( https://www.php-fig.org/psr/psr-1/ ) and [ PSR-12] ( https://www.php-fig.org/psr/psr-12/ ) code standards.
214
- ```
215
- # Check code standard adherence
216
- make cs-check
217
- ```
218
-
219
- Unit tests with [ PHPUnit] ( https://phpunit.readthedocs.io/ ) .
220
- ```
221
- # Run unit tests
222
- make test
223
- ```
224
-
225
- ## License ([ ISC] ( http://en.wikipedia.org/wiki/ISC_license ) )
226
-
227
- Copyright (C) 2014-2020 Textalk/Abicart and contributors.
228
-
229
- Websocket PHP is free software: Permission to use, copy, modify, and/or distribute this software
230
- for any purpose with or without fee is hereby granted, provided that the above copyright notice and
231
- this permission notice appear in all copies.
232
-
233
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
234
- SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
235
- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
236
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
237
- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
238
- THIS SOFTWARE.
239
-
240
- See [ Copying] ( COPYING ) .
241
-
242
58
### Contributors
243
59
244
60
Fredrik Liljegren, Armen Baghumian Sankbarani, Ruslan Bekenev,
245
61
Joshua Thijssen, Simon Lipp, Quentin Bellus, Patrick McCarren, swmcdonnell,
246
62
Ignas Bernotas, Mark Herhold, Andreas Palm, Sören Jensen, pmaasz, Alexey Stavrov,
247
63
Michael Slezak, Pierre Seznec, rmeisler, Nickolay V. Shmyrev.
248
-
249
-
250
- ## Changelog
251
-
252
- 1.4.1
253
-
254
- * Ping/Pong, handled internally to avoid breaking fragmented messages (@nshmyrev , @sirn-se )
255
- * Fix for persistent connections (@rmeisler )
256
- * Fix opcode bitmask (@peterjah )
257
-
258
- 1.4.0
259
-
260
- * Dropped support of old PHP versions (@sirn-se )
261
- * Added PSR-3 Logging support (@sirn-se )
262
- * Persistent connection option (@slezakattack )
263
- * TimeoutException on connection time out (@slezakattack )
264
-
265
- 1.3.1
266
-
267
- * Allow control messages without payload (@Logioniz )
268
- * Error code in ConnectionException (@sirn-se )
269
-
270
- 1.3.0
271
-
272
- * Implements ping/pong frames (@pmccarren @Logioniz )
273
- * Close behaviour (@sirn-se )
274
- * Various fixes concerning connection handling (@sirn-se )
275
- * Overhaul of Composer, Travis and Coveralls setup, PSR code standard and unit tests (@sirn-se )
276
-
277
- 1.2.0
278
-
279
- * Adding stream context options (to set e.g. SSL ` allow_self_signed ` ).
280
-
281
- 1.1.2
282
-
283
- * Fixed error message on broken frame.
284
-
285
- 1.1.1
286
-
287
- * Adding license information.
288
-
289
- 1.1.0
290
-
291
- * Supporting huge payloads.
292
-
293
- 1.0.3
294
-
295
- * Bugfix: Correcting address in error-message
296
-
297
- 1.0.2
298
-
299
- * Bugfix: Add port in request-header.
300
-
301
- 1.0.1
302
-
303
- * Fixing a bug from empty payloads.
304
-
305
- 1.0.0
306
-
307
- * Release as production ready.
308
- * Adding option to set/override headers.
309
- * Supporting basic authentication from user: pass in URL.
0 commit comments