zhangxiaobin 424b643451 commit 3 | 1 gadu atpakaļ | |
---|---|---|
.. | ||
examples | 1 gadu atpakaļ | |
lib | 1 gadu atpakaļ | |
tests | 1 gadu atpakaļ | |
.gitignore | 1 gadu atpakaļ | |
.travis.yml | 1 gadu atpakaļ | |
COPYING | 1 gadu atpakaļ | |
Makefile | 1 gadu atpakaļ | |
README.md | 1 gadu atpakaļ | |
codestandard.xml | 1 gadu atpakaļ | |
composer.json | 1 gadu atpakaļ | |
phpunit.xml.dist | 1 gadu atpakaļ |
This library contains WebSocket client and server for PHP.
The client and server provides methods for reading and writing to WebSocket streams. It does not include convenience operations such as listeners and implicit error handling.
Preferred way to install is with Composer.
composer require textalk/websocket
Current version support PHP versions ^7.1
.
For PHP ^5.4
and ^7.0
support use version 1.3
.
The client can read and write on a WebSocket stream. It internally supports Upgrade handshake and implicit close and ping/pong operations.
WebSocket\Client {
public __construct(string $uri, array $options = [])
public __destruct()
public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void
public receive() : mixed
public close(int $status = 1000, mixed $message = 'ttfn') : mixed
public getLastOpcode() : string
public getCloseStatus() : int
public isConnected() : bool
public setTimeout(int $seconds) : void
public setFragmentSize(int $fragment_size) : self
public getFragmentSize() : int
public setLogger(Psr\Log\LoggerInterface $logger = null) : void
}
This example send a single message to a server, and output the response.
$client = new WebSocket\Client("ws://echo.websocket.org/");
$client->send("Hello WebSocket.org!");
echo $client->receive();
$client->close();
To continuously listen to incoming messages, you need to put the receive operation within a loop. Note that these functions always throw exception on any failure, including recoverable failures such as connection time out. By consuming exceptions, the code will re-connect the socket in next loop iteration.
$client = new WebSocket\Client("ws://echo.websocket.org/");
while (true) {
try {
$message = $client->receive();
// Act on received message
// Break while loop to stop listening
} catch (\WebSocket\ConnectionException $e) {
// Possibly log errors
}
}
$client->close();
The $options
parameter in constructor accepts an associative array of options.
timeout
- Time out in seconds. Default 5 seconds.fragment_size
- Maximum payload size. Default 4096 chars.context
- A stream context created using stream_context_create.headers
- Additional headers as associative array name => content.logger
- A PSR-3 compatible logger.persistent
- Connection is re-used between requests until time out is reached. Default false.$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
$client = new WebSocket\Client("ws://echo.websocket.org/", [
'timeout' => 60, // 1 minute time out
'context' => $context,
'headers' => [
'Sec-WebSocket-Protocol' => 'soap',
'origin' => 'localhost',
],
]);
The library contains a rudimentary single stream/single thread server. It internally supports Upgrade handshake and implicit close and ping/pong operations.
Note that it does not support threading or automatic association ot continuous client requests. If you require this kind of server behavior, you need to build it on top of provided server implementation.
WebSocket\Server {
public __construct(array $options = [])
public __destruct()
public accept() : bool
public send(mixed $payload, string $opcode = 'text', bool $masked = true) : void
public receive() : mixed
public close(int $status = 1000, mixed $message = 'ttfn') : mixed
public getPort() : int
public getPath() : string
public getRequest() : array
public getHeader(string $header_name) : string|null
public getLastOpcode() : string
public getCloseStatus() : int
public isConnected() : bool
public setTimeout(int $seconds) : void
public setFragmentSize(int $fragment_size) : self
public getFragmentSize() : int
public setLogger(Psr\Log\LoggerInterface $logger = null) : void
}
This example reads a single message from a client, and respond with the same message.
$server = new WebSocket\Server();
$server->accept();
$message = $server->receive();
$server->send($message);
$server->close();
To continuously listen to incoming messages, you need to put the receive operation within a loop. Note that these functions always throw exception on any failure, including recoverable failures such as connection time out. By consuming exceptions, the code will re-connect the socket in next loop iteration.
$server = new WebSocket\Server();
while ($server->accept()) {
try {
$message = $server->receive();
// Act on received message
// Break while loop to stop listening
} catch (\WebSocket\ConnectionException $e) {
// Possibly log errors
}
}
$server->close();
The $options
parameter in constructor accepts an associative array of options.
timeout
- Time out in seconds. Default 5 seconds.port
- The server port to listen to. Default 8000.fragment_size
- Maximum payload size. Default 4096 chars.logger
- A PSR-3 compatible logger.$server = new WebSocket\Server([
'timeout' => 60, // 1 minute time out
'port' => 9000,
]);
WebSocket\BadOpcodeException
- Thrown if provided opcode is invalid.WebSocket\BadUriException
- Thrown if provided URI is invalid.WebSocket\ConnectionException
- Thrown on any socket I/O failure.WebSocket\TimeoutExeception
- Thrown when the socket experiences a time out.Requirements on pull requests;
Install or update dependencies using Composer.
# Install dependencies
make install
# Update dependencies
make update
This project uses PSR-1 and PSR-12 code standards.
# Check code standard adherence
make cs-check
Unit tests with PHPUnit.
# Run unit tests
make test
Copyright (C) 2014-2020 Textalk/Abicart and contributors.
Websocket PHP is free software: Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
See Copying.
Fredrik Liljegren, Armen Baghumian Sankbarani, Ruslan Bekenev, Joshua Thijssen, Simon Lipp, Quentin Bellus, Patrick McCarren, swmcdonnell, Ignas Bernotas, Mark Herhold, Andreas Palm, Sören Jensen, pmaasz, Alexey Stavrov, Michael Slezak, Pierre Seznec, rmeisler, Nickolay V. Shmyrev.
1.4.1
1.4.0
1.3.1
1.3.0
1.2.0
allow_self_signed
).1.1.2
1.1.1
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0