11#Pawl
22
3+ [ ![ Autobahn Testsuite] ( https://img.shields.io/badge/Autobahn-passing-brightgreen.svg )] ( http://socketo.me/reports/pawl/index.html )
4+
35An asynchronous PHP WebSocket client
46
57---
8+ Using Pawl as a standalone app: Connect to an echo server, send a message, display output back, close connection.
9+ ``` php
10+ <?php
11+
12+ require __DIR__ . '/vendor/autoload.php';
13+
14+ \Ratchet\Client\connect('ws://echo.socketo.me:9000')->then(function($conn) {
15+ $conn->on('message', function($msg) use ($conn) {
16+ echo "Received: {$msg}\n";
17+ $conn->close();
18+ });
619
20+ $conn->send('Hello World!');
21+ }, function ($e) {
22+ echo "Could not connect: {$e->getMessage()}\n";
23+ });
24+ ```
25+
26+ Using the components of Pawl: Requesting sub-protocols, and sending custom headers while using a specific React Event Loop.
727``` php
828<?php
929
@@ -12,13 +32,19 @@ An asynchronous PHP WebSocket client
1232 $loop = React\EventLoop\Factory::create();
1333 $connector = new Ratchet\Client\Connector($loop);
1434
15- $connector('ws://127.0.0.1:8080')->then(function(Ratchet\Client\WebSocket $conn) {
16- $conn->on('message', function($msg) {
35+ $connector('ws://127.0.0.1:9000', ['protocol1', 'subprotocol2'], ['Origin' => 'http://localhost'])
36+ ->then(function(Ratchet\Client\WebSocket $conn) {
37+ $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
1738 echo "Received: {$msg}\n";
39+ $conn->close();
40+ });
41+
42+ $conn->on('close', function($code = null, $reason = null) {
43+ echo "Connection closed ({$code} - {$reason})\n";
1844 });
1945
2046 $conn->send('Hello World!');
21- }, function($e) use ($loop) {
47+ }, function(\Exception $e) use ($loop) {
2248 echo "Could not connect: {$e->getMessage()}\n";
2349 $loop->stop();
2450 });
0 commit comments