Skip to content

Commit d6c5a99

Browse files
committed
Documentation
1 parent 718364f commit d6c5a99

12 files changed

+375
-296
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.phpunit.result.cache
23
build/
34
composer.lock
45
composer.phar

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 8.0
54
- 7.4
65
- 7.3
76
- 7.2

README.md

+13-259
Original file line numberDiff line numberDiff line change
@@ -8,140 +8,45 @@ This library contains WebSocket client and server for PHP.
88
The client and server provides methods for reading and writing to WebSocket streams.
99
It does not include convenience operations such as listeners and implicit error handling.
1010

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+
1119
## Installing
1220

1321
Preferred way to install is with [Composer](https://getcomposer.org/).
1422
```
1523
composer require textalk/websocket
1624
```
1725

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`.
2029

2130
## Client
2231

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.
2433
It internally supports Upgrade handshake and implicit close and ping/pong operations.
2534

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-
5235
```php
5336
$client = new WebSocket\Client("ws://echo.websocket.org/");
5437
$client->send("Hello WebSocket.org!");
5538
echo $client->receive();
5639
$client->close();
5740
```
5841

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-
10542
## Server
10643

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).
10845
It internally supports Upgrade handshake and implicit close and ping/pong operations.
10946

11047
Note that it does **not** support threading or automatic association ot continuous client requests.
11148
If you require this kind of server behavior, you need to build it on top of provided server implementation.
11249

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-
14550
```php
14651
$server = new WebSocket\Server();
14752
$server->accept();
@@ -150,160 +55,9 @@ $server->send($message);
15055
$server->close();
15156
```
15257

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-
24258
### Contributors
24359

24460
Fredrik Liljegren, Armen Baghumian Sankbarani, Ruslan Bekenev,
24561
Joshua Thijssen, Simon Lipp, Quentin Bellus, Patrick McCarren, swmcdonnell,
24662
Ignas Bernotas, Mark Herhold, Andreas Palm, Sören Jensen, pmaasz, Alexey Stavrov,
24763
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.

COPYING docs/COPYING.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# Websocket: License
2+
13
Websocket PHP is free software released under the following license:
24

3-
ISC License
5+
[ISC License](http://en.wikipedia.org/wiki/ISC_license)
46

57
Permission to use, copy, modify, and/or distribute this software for any purpose with or without
68
fee is hereby granted, provided that the above copyright notice and this permission notice appear

0 commit comments

Comments
 (0)