Skip to content

Commit 53aacc3

Browse files
committed
Merge pull request ratchetphp#24 from ratchetphp/doc-n-functions
Shortcuts and documentation
2 parents 7c39664 + a31fec7 commit 53aacc3

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2011-2014 Chris Boden
1+
Copyright (c) 2015 Chris Boden
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
#Pawl
22

3+
[![Autobahn Testsuite](https://img.shields.io/badge/Autobahn-passing-brightgreen.svg)](http://socketo.me/reports/pawl/index.html)
4+
35
An 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
});

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"name": "ratchet/pawl"
33
, "description": "Asynchronous WebSocket client"
4-
, "keywords": ["WebSocket", "client", "Ratchet", "async"]
4+
, "keywords": ["WebSocket", "client", "Ratchet", "async", "websocket client"]
55
, "license": "MIT"
66
, "autoload": {
77
"psr-4": {
88
"Ratchet\\Client\\": "src"
99
}
10+
, "files": ["src/functions_include.php"]
1011
}
1112
, "require": {
1213
"php": ">=5.4"
1314
, "react/socket-client": "0.4.*"
14-
, "ratchet/rfc6455": "0.2.*"
15+
, "ratchet/rfc6455": "^0.2.1"
1516
}
1617

1718
}

src/Connector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public function __construct(LoopInterface $loop, Resolver $resolver = null) {
2929
$this->_negotiator = new ClientNegotiator;
3030
}
3131

32+
/**
33+
* @param string $url
34+
* @param array $subProtocols
35+
* @param array $headers
36+
* @return \React\Promise\PromiseInterface
37+
*/
3238
public function __invoke($url, array $subProtocols = [], array $headers = []) {
3339
try {
3440
$request = $this->generateRequest($url, $subProtocols, $headers);
@@ -92,6 +98,12 @@ public function __invoke($url, array $subProtocols = [], array $headers = []) {
9298
});
9399
}
94100

101+
/**
102+
* @param string $url
103+
* @param array $subProtocols
104+
* @param array $headers
105+
* @return \Psr\Http\Message\RequestInterface
106+
*/
95107
protected function generateRequest($url, array $subProtocols, array $headers) {
96108
$uri = gPsr\uri_for($url);
97109

src/WebSocket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class WebSocket implements EventEmitterInterface {
4242
* @param \Psr\Http\Message\ResponseInterface $response
4343
* @param \Psr\Http\Message\RequestInterface $request
4444
* @event message
45-
* @event end
45+
* @event pong
4646
* @event close
4747
* @event error
4848
*/

src/functions.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Ratchet\Client;
3+
use React\EventLoop\LoopInterface;
4+
use React\EventLoop\Factory as ReactFactory;
5+
6+
/**
7+
* @param string $url
8+
* @param array $subProtocols
9+
* @param array $headers
10+
* @param LoopInterface|null $loop
11+
* @return \React\Promise\PromiseInterface
12+
*/
13+
function connect($url, array $subProtocols = [], $headers = [], LoopInterface $loop = null) {
14+
$loop = $loop ?: ReactFactory::create();
15+
16+
$connector = new Connector($loop);
17+
$connection = $connector($url, $subProtocols, $headers);
18+
19+
register_shutdown_function(function() use ($loop) {
20+
$loop->run();
21+
});
22+
23+
return $connection;
24+
}

src/functions_include.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
if (!function_exists('Ratchet\Client\connect')) {
4+
require __DIR__ . '/functions.php';
5+
}

0 commit comments

Comments
 (0)