@@ -10,24 +10,23 @@ An asynchronous WebSocket client in PHP
10
10
11
11
#### Usage
12
12
Pawl as a standalone app: Connect to an echo server, send a message, display output, close connection:
13
+
13
14
``` php
14
15
<?php
15
16
16
- require __DIR__ . '/vendor/autoload.php';
17
-
18
- \Ratchet\Client\connect('wss://echo.websocket.org:443')->then(function($conn) {
19
- $conn->on('message', function($msg) use ($conn) {
20
- echo "Received: {$msg}\n";
21
- $conn->close();
22
- });
17
+ require __DIR__ . '/vendor/autoload.php';
23
18
24
- $conn->send('Hello World!');
25
- }, function ($e) {
26
- echo "Could not connect: {$e->getMessage()}\n";
19
+ \Ratchet\Client\connect('wss://echo.websocket.org:443')->then(function($conn) {
20
+ $conn->on('message', function($msg) use ($conn) {
21
+ echo "Received: {$msg}\n";
22
+ $conn->close();
27
23
});
28
- ```
29
24
30
- ---
25
+ $conn->send('Hello World!');
26
+ }, function ($e) {
27
+ echo "Could not connect: {$e->getMessage()}\n";
28
+ });
29
+ ```
31
30
32
31
#### Classes
33
32
@@ -57,31 +56,29 @@ A more in-depth example using explicit interfaces: Requesting sub-protocols, and
57
56
``` php
58
57
<?php
59
58
60
- require __DIR__ . '/vendor/autoload.php';
61
-
62
- $loop = \React\EventLoop\Factory::create();
63
- $reactConnector = new \React\Socket\Connector($loop, [
64
- 'dns' => '8.8.8.8',
65
- 'timeout' => 10
66
- ]);
67
- $connector = new \Ratchet\Client\Connector($loop, $reactConnector);
68
-
69
- $connector('ws://127.0.0.1:9000', ['protocol1', 'subprotocol2'], ['Origin' => 'http://localhost'])
70
- ->then(function(\Ratchet\Client\WebSocket $conn) {
71
- $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
72
- echo "Received: {$msg}\n";
73
- $conn->close();
74
- });
75
-
76
- $conn->on('close', function($code = null, $reason = null) {
77
- echo "Connection closed ({$code} - {$reason})\n";
78
- });
79
-
80
- $conn->send('Hello World!');
81
- }, function(\Exception $e) use ($loop) {
82
- echo "Could not connect: {$e->getMessage()}\n";
83
- $loop->stop();
59
+ require __DIR__ . '/vendor/autoload.php';
60
+
61
+ $reactConnector = new \React\Socket\Connector([
62
+ 'dns' => '8.8.8.8',
63
+ 'timeout' => 10
64
+ ]);
65
+ $loop = \React\EventLoop\Loop::get();
66
+ $connector = new \Ratchet\Client\Connector($loop, $reactConnector);
67
+
68
+ $connector('ws://127.0.0.1:9000', ['protocol1', 'subprotocol2'], ['Origin' => 'http://localhost'])
69
+ ->then(function(\Ratchet\Client\WebSocket $conn) {
70
+ $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
71
+ echo "Received: {$msg}\n";
72
+ $conn->close();
73
+ });
74
+
75
+ $conn->on('close', function($code = null, $reason = null) {
76
+ echo "Connection closed ({$code} - {$reason})\n";
84
77
});
85
78
86
- $loop->run();
79
+ $conn->send('Hello World!');
80
+ }, function(\Exception $e) use ($loop) {
81
+ echo "Could not connect: {$e->getMessage()}\n";
82
+ $loop->stop();
83
+ });
87
84
```
0 commit comments