-
Notifications
You must be signed in to change notification settings - Fork 441
WAMP #573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
WAMP #573
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f99c2c1
wamp
ASKozienko 5e2d4ef
wamp
ASKozienko 99778d7
wamp
ASKozienko a512b53
wamp
ASKozienko 18273bf
wamp
ASKozienko 49538de
wamp
ASKozienko 3d406cc
wamp
ASKozienko 3364d61
wamp
ASKozienko ba418ad
wamp
ASKozienko 24f8f90
wamp
ASKozienko 715bb7f
wamp
ASKozienko 74c1339
wamp
ASKozienko 3ebdb43
wamp
ASKozienko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -66,6 +66,7 @@ remote async-event-dispatcher [email protected]:php-enqueue/async-event-dispatcher. | |
remote async-command [email protected]:php-enqueue/async-command.git | ||
remote mongodb [email protected]:php-enqueue/mongodb.git | ||
remote dsn [email protected]:php-enqueue/dsn.git | ||
remote wamp [email protected]:php-enqueue/wamp.git | ||
|
||
split 'pkg/enqueue' enqueue | ||
split 'pkg/simple-client' simple-client | ||
|
@@ -90,3 +91,4 @@ split 'pkg/async-event-dispatcher' async-event-dispatcher | |
split 'pkg/async-command' async-command | ||
split 'pkg/mongodb' mongodb | ||
split 'pkg/dsn' dsn | ||
split 'pkg/wamp' wamp |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
require __DIR__.'/../../vendor/autoload.php'; | ||
|
||
use Thruway\Peer\Router; | ||
use Thruway\Transport\RatchetTransportProvider; | ||
|
||
$router = new Router(); | ||
|
||
$transportProvider = new RatchetTransportProvider('0.0.0.0', 9090); | ||
|
||
$router->addTransportProvider($transportProvider); | ||
|
||
$router->start(); |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Web Application Messaging Protocol (WAMP) Transport | ||
|
||
A transport for [Web Application Messaging Protocol](https://wamp-proto.org/). | ||
WAMP is an open standard WebSocket subprotocol. | ||
It uses internally Thruway PHP library [voryx/thruway](https://github.com/voryx/Thruway) | ||
|
||
* [Installation](#installation) | ||
* [Start the WAMP router](#start-the-wamp-router) | ||
* [Create context](#create-context) | ||
* [Consume message](#consume-message) | ||
* [Subscription consumer](#subscription-consumer) | ||
* [Send message to topic](#send-message-to-topic) | ||
|
||
## Installation | ||
|
||
```bash | ||
$ composer require enqueue/wamp | ||
``` | ||
|
||
## Start the WAMP router | ||
|
||
```bash | ||
$ php vendor/voryx/thruway/Examples/SimpleWsRouter.php | ||
``` | ||
|
||
Thruway is now running on 127.0.0.1 port 9090 | ||
|
||
|
||
## Create context | ||
|
||
```php | ||
<?php | ||
use Enqueue\Wamp\WampConnectionFactory; | ||
|
||
$connectionFactory = new WampConnectionFactory(); | ||
|
||
$context = $connectionFactory->createContext(); | ||
|
||
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN | ||
$context = (new \Enqueue\ConnectionFactoryFactory())->create('wamp:')->createContext(); | ||
``` | ||
|
||
## Consume message: | ||
|
||
Start message consumer before send message to the topic | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\Wamp\WampContext $context */ | ||
|
||
$fooTopic = $context->createTopic('foo'); | ||
|
||
$consumer = $context->createConsumer($fooQueue); | ||
|
||
while (true) { | ||
if ($message = $consumer->receive()) { | ||
// process a message | ||
} | ||
} | ||
``` | ||
|
||
## Subscription consumer | ||
|
||
```php | ||
<?php | ||
use Interop\Queue\Message; | ||
use Interop\Queue\Consumer; | ||
|
||
/** @var \Enqueue\Wamp\WampContext $context */ | ||
/** @var \Enqueue\Wamp\WampDestination $fooQueue */ | ||
/** @var \Enqueue\Wamp\WampDestination $barQueue */ | ||
|
||
$fooConsumer = $context->createConsumer($fooQueue); | ||
$barConsumer = $context->createConsumer($barQueue); | ||
|
||
$subscriptionConsumer = $context->createSubscriptionConsumer(); | ||
$subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) { | ||
// process message | ||
|
||
return true; | ||
}); | ||
$subscriptionConsumer->subscribe($barConsumer, function(Message $message, Consumer $consumer) { | ||
// process message | ||
|
||
return true; | ||
}); | ||
|
||
$subscriptionConsumer->consume(2000); // 2 sec | ||
``` | ||
|
||
## Send message to topic | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\Wamp\WampContext $context */ | ||
|
||
$fooTopic = $context->createTopic('foo'); | ||
$message = $context->createMessage('Hello world!'); | ||
|
||
$context->createProducer()->send($fooTopic, $message); | ||
``` | ||
|
||
[back to index](../index.md) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Enqueue\Test; | ||
|
||
use Enqueue\Wamp\WampConnectionFactory; | ||
use Enqueue\Wamp\WampContext; | ||
|
||
trait WampExtension | ||
{ | ||
private function buildWampContext(): WampContext | ||
{ | ||
if (false == $dsn = getenv('WAMP_DSN')) { | ||
throw new \PHPUnit_Framework_SkippedTestError('Functional tests are not allowed in this environment'); | ||
} | ||
|
||
return (new WampConnectionFactory($dsn))->createContext(); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*~ | ||
/composer.lock | ||
/composer.phar | ||
/phpunit.xml | ||
/vendor/ | ||
/.idea/ |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
sudo: false | ||
|
||
git: | ||
depth: 10 | ||
|
||
language: php | ||
|
||
php: | ||
- '7.1' | ||
- '7.2' | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
install: | ||
- composer self-update | ||
- composer install | ||
|
||
script: | ||
- vendor/bin/phpunit --exclude-group=functional |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Enqueue\Wamp; | ||
|
||
class JsonSerializer implements Serializer | ||
{ | ||
public function toString(WampMessage $message): string | ||
{ | ||
$json = json_encode([ | ||
'body' => $message->getBody(), | ||
'properties' => $message->getProperties(), | ||
'headers' => $message->getHeaders(), | ||
]); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The malformed json given. Error %s and message %s', | ||
json_last_error(), | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return $json; | ||
} | ||
|
||
public function toMessage(string $string): WampMessage | ||
{ | ||
$data = json_decode($string, true); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The malformed json given. Error %s and message %s', | ||
json_last_error(), | ||
json_last_error_msg() | ||
)); | ||
} | ||
|
||
return new WampMessage($data['body'], $data['properties'], $data['headers']); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2018 Forma-Pro | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.