devn00b c2c6bf7610 Initial Upload of Bot | 1 年之前 | |
---|---|---|
.. | ||
.github | 1 年之前 | |
src | 1 年之前 | |
tests | 1 年之前 | |
.gitignore | 1 年之前 | |
LICENSE | 1 年之前 | |
README.md | 1 年之前 | |
composer.json | 1 年之前 | |
phpunit.xml.dist | 1 年之前 | |
phpunit.xml.legacy | 1 年之前 |
An asynchronous WebSocket client in PHP
composer require ratchet/pawl
Pawl as a standalone app: Connect to an echo server, send a message, display output, close connection:
<?php
require __DIR__ . '/vendor/autoload.php';
\Ratchet\Client\connect('wss://echo.websocket.org:443')->then(function($conn) {
$conn->on('message', function($msg) use ($conn) {
echo "Received: {$msg}\n";
$conn->close();
});
$conn->send('Hello World!');
}, function ($e) {
echo "Could not connect: {$e->getMessage()}\n";
});
There are 3 primary classes to be aware of and use in Pawl:
Makes HTTP requests to servers returning a promise that, if successful, will resolve to a WebSocket object. A connector is configured via its constructor and a request is made by invoking the class. Multiple connections can be established through a single connector. The invoke mehtod has 3 parameters:
Origin
This is the object used to interact with a WebSocket server. It has two methods: send
and close
.
It has two public properties: request
and response
which are PSR-7 objects representing the client and server side HTTP handshake headers used to establish the WebSocket connection.
This is the object received from a WebSocket server. It has a __toString
method which is how most times you will want to access the data received.
If you need to do binary messaging you will most likely need to use methods on the object.
A more in-depth example using explicit interfaces: Requesting sub-protocols, and sending custom headers while using a specific React Event Loop:
<?php
require __DIR__ . '/vendor/autoload.php';
$reactConnector = new \React\Socket\Connector([
'dns' => '8.8.8.8',
'timeout' => 10
]);
$loop = \React\EventLoop\Loop::get();
$connector = new \Ratchet\Client\Connector($loop, $reactConnector);
$connector('ws://127.0.0.1:9000', ['protocol1', 'subprotocol2'], ['Origin' => 'http://localhost'])
->then(function(\Ratchet\Client\WebSocket $conn) {
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
echo "Received: {$msg}\n";
$conn->close();
});
$conn->on('close', function($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});
$conn->send('Hello World!');
}, function(\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});