-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
38 additions
and
140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
# websocket-client | ||
|
||
![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square) | ||
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. | ||
`amphp/websocket-client` provides an asynchronous WebSocket client for PHP based on Amp. | ||
Websockets are full-duplex communication channels, which are mostly used for realtime communication where the HTTP request / response cycle has too much overhead. | ||
They're also used if the server should be able to push data to the client without an explicit request. | ||
|
||
`amphp/websocket-client` is an async WebSocket client for PHP based on Amp. | ||
There are various use cases for a WebSocket client in PHP, such as consuming realtime APIs, writing tests for a WebSocket server, or controlling web browsers via their remote debugging APIs, which are based on WebSockets. | ||
|
||
## Installation | ||
|
||
|
@@ -16,9 +19,39 @@ composer require amphp/websocket-client | |
|
||
* PHP 8.1+ | ||
|
||
## Documentation & Examples | ||
|
||
More extensive code examples reside in the [`examples`](examples) directory. | ||
## Connecting | ||
|
||
You can create new WebSocket connections using `Amp\Websocket\connect()`. | ||
It accepts a string as first argument, which must use the `ws` or `wss` (WebSocket over TLS) scheme. | ||
Options can be specified by passing a `WebsocketHandshake` object instead of a string as first argument, which can also be used to pass additional headers with the initial handshake. | ||
The second argument is an optional `Cancellation`. | ||
|
||
```php | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Amp\Websocket\Client; | ||
|
||
$connection = Client\connect('ws://localhost:1337/ws'); | ||
|
||
// do something | ||
``` | ||
|
||
## Sending Data | ||
|
||
WebSocket messages can be sent using the `sendText()` and `sendBinary()` methods. | ||
Text messages sent with `sendText()` must be valid UTF-8. | ||
Binary messages send with `sendBinary()` can be arbitrary data. | ||
|
||
Both methods return as soon as the message has been fully written to the send buffer. This doesn't mean that the message has been received by the other party or that the message even left the local system's send buffer, yet. | ||
|
||
## Receiving Data | ||
|
||
WebSocket messages can be received using the `receive()` method. `receive()` returns once the client has started to receive a message. This allows streaming WebSocket messages, which might be pretty large. In practice, most messages are rather small, and it's fine buffering them completely. `receive()` returns to a `WebsocketMessage`, which allows easy buffered and streamed consumption. | ||
|
||
## Example | ||
|
||
```php | ||
use Amp\Websocket\Client\WebsocketHandshake; | ||
|
@@ -48,7 +81,7 @@ while ($message = $connection->receive()) { | |
|
||
## Security | ||
|
||
If you discover any security related issues, please email [`[email protected]`](mailto:[email protected]) instead of using the issue tracker. | ||
If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker. | ||
|
||
## License | ||
|
||
|
This file was deleted.
Oops, something went wrong.
Submodule .shared
deleted from
a965e8
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.