Skip to content

Commit 9fbb3b4

Browse files
authored
Merge pull request #81 from Textalk/v1.3-docs
V1.3 docs
2 parents 0eaa881 + a722a7d commit 9fbb3b4

File tree

1 file changed

+188
-35
lines changed

1 file changed

+188
-35
lines changed

README.md

Lines changed: 188 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
Websocket Client for PHP
2-
========================
1+
# Websocket Client and Server for PHP
32

4-
[![Build Status](https://travis-ci.org/Textalk/websocket-php.png)](https://travis-ci.org/Textalk/websocket-php)
5-
[![Coverage Status](https://coveralls.io/repos/Textalk/websocket-php/badge.png)](https://coveralls.io/r/Textalk/websocket-php)
3+
[![Build Status](https://travis-ci.org/Textalk/websocket-php.svg?branch=master)](https://travis-ci.org/Textalk/websocket-php)
4+
[![Coverage Status](https://coveralls.io/repos/github/Textalk/websocket-php/badge.svg?branch=master)](https://coveralls.io/github/Textalk/websocket-php)
65

7-
This package mainly contains a WebSocket client for PHP.
6+
This library contains WebSocket client and server for PHP.
87

9-
I made it because the state of other WebSocket clients I could found was either very poor
10-
(sometimes failing on large frames) or had huge dependencies (React…).
8+
The client and server provides methods for reading and writing to WebSocket streams.
9+
It does not include convenience operations such as listeners and implicit error handling.
1110

12-
The Client should be good.
13-
14-
The Server there because much of the code would be identical in writing a server, and because it is
15-
used for the tests. To be really useful though, there should be a Connection-class returned from a
16-
new Connection, and the Server-class only handling the handshake. Then you could hold a full array
17-
of Connections and check them periodically for new data, send something to them all or fork off a
18-
process handling one connection. But, I have no use for that right now. (Actually, I would
19-
suggest a language with better asynchronous handling than PHP for that.)
20-
21-
Installing
22-
----------
11+
## Installing
2312

2413
Preferred way to install is with [Composer](https://getcomposer.org/).
2514
```
@@ -28,22 +17,177 @@ composer require textalk/websocket
2817

2918
Currently support PHP versions `^5.4` and `^7.0`.
3019

20+
## Client
21+
22+
The client can read and write on a WebSocket stream.
23+
It internally supports Upgrade handshake and implicit close and ping/pong operations.
24+
25+
### Class synopsis
3126

32-
Client usage:
33-
-------------
3427
```php
35-
require('vendor/autoload.php');
28+
WebSocket\Client {
29+
30+
public __construct(string $uri, array $options = [])
31+
public __destruct()
32+
33+
public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void
34+
public receive() : mixed
35+
public close(int $status = 1000, mixed $message = 'ttfn') : mixed
36+
37+
public getLastOpcode() : string
38+
public getCloseStatus() : int
39+
public isConnected() : bool
40+
public setTimeout(int $seconds) : void
41+
public setFragmentSize(int $fragment_size) : self
42+
public getFragmentSize() : int
43+
}
44+
```
45+
46+
### Example: Simple send-receive operation
3647

37-
use WebSocket\Client;
48+
This example send a single message to a server, and output the response.
3849

39-
$client = new Client("ws://echo.websocket.org/");
50+
```php
51+
$client = new WebSocket\Client("ws://echo.websocket.org/");
4052
$client->send("Hello WebSocket.org!");
53+
echo $client->receive();
54+
$client->close();
55+
```
56+
57+
### Example: Listening to a server
58+
59+
To continuously listen to incoming messages, you need to put the receive operation within a loop.
60+
Note that these functions **always** throw exception on any failure, including recoverable failures such as connection time out.
61+
By consuming exceptions, the code will re-connect the socket in next loop iteration.
62+
63+
```php
64+
$client = new WebSocket\Client("ws://echo.websocket.org/");
65+
while (true) {
66+
try {
67+
$message = $client->receive();
68+
// Act on received message
69+
// Break while loop to stop listening
70+
} catch (\WebSocket\ConnectionException $e) {
71+
// Possibly log errors
72+
}
73+
}
74+
$client->close();
75+
```
76+
77+
### Constructor options
78+
79+
The `$options` parameter in constructor accepts an associative array of options.
80+
81+
* `timeout` - Time out in seconds. Default 5 seconds.
82+
* `fragment_size` - Maximum payload size. Default 4096 chars.
83+
* `context` - A stream context created using [stream_context_create](https://www.php.net/manual/en/function.stream-context-create).
84+
* `headers` - Additional headers as associative array name => content.
85+
86+
```php
87+
$context = stream_context_create();
88+
stream_context_set_option($context, 'ssl', 'verify_peer', false);
89+
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
90+
91+
$client = new WebSocket\Client("ws://echo.websocket.org/", [
92+
'timeout' => 60, // 1 minute time out
93+
'context' => $context,
94+
'headers' => [
95+
'Sec-WebSocket-Protocol' => 'soap',
96+
'origin' => 'localhost',
97+
],
98+
]);
99+
```
100+
101+
## Server
102+
103+
The library contains a rudimentary single stream/single thread server.
104+
It internally supports Upgrade handshake and implicit close and ping/pong operations.
41105

42-
echo $client->receive(); // Will output 'Hello WebSocket.org!'
106+
Note that it does **not** support threading or automatic association ot continuous client requests.
107+
If you require this kind of server behavior, you need to build it on top of provided server implementation.
108+
109+
### Class synopsis
110+
111+
```php
112+
WebSocket\Server {
113+
114+
public __construct(array $options = [])
115+
public __destruct()
116+
117+
public accept() : bool
118+
public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void
119+
public receive() : mixed
120+
public close(int $status = 1000, mixed $message = 'ttfn') : mixed
121+
122+
public getPort() : int
123+
public getPath() : string
124+
public getRequest() : array
125+
public getHeader(string $header_name) : string|null
126+
127+
public getLastOpcode() : string
128+
public getCloseStatus() : int
129+
public isConnected() : bool
130+
public setTimeout(int $seconds) : void
131+
public setFragmentSize(int $fragment_size) : self
132+
public getFragmentSize() : int
133+
}
43134
```
44135

45-
Development and contribution
46-
-----------------
136+
### Example: Simple receive-send operation
137+
138+
This example reads a single message from a client, and respond with the same message.
139+
140+
```php
141+
$server = new WebSocket\Server();
142+
$server->accept();
143+
$message = $server->receive();
144+
$server->send($message);
145+
$server->close();
146+
```
147+
148+
### Example: Listening to clients
149+
150+
To continuously listen to incoming messages, you need to put the receive operation within a loop.
151+
Note that these functions **always** throw exception on any failure, including recoverable failures such as connection time out.
152+
By consuming exceptions, the code will re-connect the socket in next loop iteration.
153+
154+
```php
155+
$server = new WebSocket\Server();
156+
while ($server->accept()) {
157+
try {
158+
$message = $server->receive();
159+
// Act on received message
160+
// Break while loop to stop listening
161+
} catch (\WebSocket\ConnectionException $e) {
162+
// Possibly log errors
163+
}
164+
}
165+
$server->close();
166+
```
167+
168+
### Constructor options
169+
170+
The `$options` parameter in constructor accepts an associative array of options.
171+
172+
* `timeout` - Time out in seconds. Default 5 seconds.
173+
* `port` - The server port to listen to. Default 8000.
174+
* `fragment_size` - Maximum payload size. Default 4096 chars.
175+
176+
```php
177+
$server = new WebSocket\Server([
178+
'timeout' => 60, // 1 minute time out
179+
'port' => 9000,
180+
]);
181+
```
182+
183+
## Exceptions
184+
185+
* `WebSocket\BadOpcodeException` - Thrown if provided opcode is invalid.
186+
* `WebSocket\BadUriException` - Thrown if provided URI is invalid.
187+
* `WebSocket\ConnectionException` - Thrown on any socket I/O failure.
188+
189+
190+
## Development and contribution
47191

48192
Install or update dependencies using [Composer](https://getcomposer.org/).
49193
```
@@ -66,13 +210,9 @@ Unit tests with [PHPUnit](https://phpunit.readthedocs.io/).
66210
make test
67211
```
68212

69-
License ([ISC](http://en.wikipedia.org/wiki/ISC_license))
70-
---------------------------------------------------------
213+
## License ([ISC](http://en.wikipedia.org/wiki/ISC_license))
71214

72-
Copyright (C) 2014-2020 Textalk/Abicart
73-
Copyright (C) 2015 Patrick McCarren - added payload fragmentation for huge payloads
74-
Copyright (C) 2015 Ignas Bernotas - added stream context options
75-
Copyright (C) 2015 Patrick McCarren - added ping/pong support
215+
Copyright (C) 2014-2020 Textalk/Abicart and contributors.
76216

77217
Websocket PHP is free software: Permission to use, copy, modify, and/or distribute this software
78218
for any purpose with or without fee is hereby granted, provided that the above copyright notice and
@@ -85,11 +225,24 @@ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
85225
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
86226
THIS SOFTWARE.
87227

88-
See COPYING.
228+
See [Copying](COPYING).
229+
230+
### Contributors
231+
232+
Fredrik Liljegren, Armen Baghumian Sankbarani, Ruslan Bekenev,
233+
Joshua Thijssen, Simon Lipp, Quentin Bellus, Patrick McCarren, swmcdonnell,
234+
Ignas Bernotas, Mark Herhold, Andreas Palm, Sören Jensen, pmaasz, Alexey Stavrov.
235+
236+
237+
## Changelog
89238

239+
1.3.0
90240

91-
Changelog
92-
---------
241+
* Implements ping/pong frames (@pmccarren)
242+
* Correct ping-pong behaviour (@Logioniz)
243+
* Correct close behaviour (@sirn-se)
244+
* Various fixes concerning connection handling (@sirn-se)
245+
* Overhaul of Composer, Travis and Coveralls setup, PSR code standard and unit tests (@sirn-se)
93246

94247
1.2.0
95248

0 commit comments

Comments
 (0)