Skip to content

Commit f7c36ae

Browse files
author
prophet777
committed
Add websocket pusher
1 parent 21395bc commit f7c36ae

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

DependencyInjection/Configuration.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,43 @@ public function getConfigTreeBuilder()
119119
->arrayNode('pushers')
120120
->append($this->addZmqNode())
121121
->append($this->addAmqpNode())
122+
->append($this->addWampNode())
122123
->end()
123124
->end()
124125
->end();
125126

126127
return $treeBuilder;
127128
}
128129

130+
protected function addWampNode()
131+
{
132+
$builder = new TreeBuilder();
133+
$node = $builder->root('wamp');
134+
135+
$node
136+
->addDefaultsIfNotSet()
137+
->children()
138+
->scalarNode('host')
139+
->example('127.0.0.1')
140+
->isRequired()
141+
->cannotBeEmpty()
142+
->end()
143+
->scalarNode('port')
144+
->example('1337')
145+
->isRequired()
146+
->cannotBeEmpty()
147+
->end()
148+
->booleanNode('ssl')
149+
->defaultValue(false)
150+
->end()
151+
->scalarNode('origin')
152+
->defaultValue(null)
153+
->end()
154+
->end();
155+
156+
return $node;
157+
}
158+
129159
protected function addZmqNode()
130160
{
131161
$builder = new TreeBuilder();

Pusher/AbstractPusher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public function isConnected()
106106
*/
107107
public function push($data, $routeName, $routeParameters, Array $context = [])
108108
{
109-
$chanel = $this->router->generate($routeName, $routeParameters);
110-
$message = new Message($chanel, $data);
109+
$channel = $this->router->generate($routeName, $routeParameters);
110+
$message = new Message($channel, $data);
111111

112112
return $this->doPush($this->serializer->serialize($message), $context);
113113
}

Pusher/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Message implements MessageInterface
1616
*/
1717
public function __construct($topic, $data)
1818
{
19-
$this->name = $topic;
19+
$this->topic = $topic;
2020
$this->data = $data;
2121
}
2222

@@ -25,7 +25,7 @@ public function __construct($topic, $data)
2525
*/
2626
public function getTopic()
2727
{
28-
return $this->name;
28+
return $this->topic;
2929
}
3030

3131
/**

Pusher/Wamp/WampPusher.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Gos\Bundle\WebSocketBundle\Pusher\Wamp;
4+
5+
use Gos\Bundle\WebSocketBundle\Pusher\AbstractPusher;
6+
use Gos\Bundle\WebSocketBundle\Pusher\Serializer\MessageSerializer;
7+
use Gos\Component\WebSocketClient\Wamp\Client;
8+
9+
class WampPusher extends AbstractPusher
10+
{
11+
/** @var MessageSerializer */
12+
protected $serializer;
13+
14+
/**
15+
* @param MessageSerializer $serializer
16+
*/
17+
public function __construct(MessageSerializer $serializer)
18+
{
19+
$this->serializer = $serializer;
20+
}
21+
22+
/**
23+
* @param string $data
24+
* @param array $context
25+
*/
26+
protected function doPush($data, array $context)
27+
{
28+
if (false === $this->isConnected()) {
29+
$config = $this->getConfig();
30+
31+
$this->connection = new Client($config['host'], $config['port'], $config['ssl'], $config['origin']);
32+
$this->connection->connect('/');
33+
$this->setConnected();
34+
}
35+
36+
$message = $this->serializer->deserialize($data);
37+
38+
$this->connection->publish($message->getTopic(), json_encode($message->getData()));
39+
}
40+
41+
public function close()
42+
{
43+
if (false === $this->isConnected()) {
44+
return;
45+
}
46+
47+
$this->connection->disconnect();
48+
}
49+
}

Resources/config/services/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ services:
213213
tags:
214214
- { name: gos_web_socket.pusher, alias: amqp }
215215

216+
gos_web_socket.wamp.pusher:
217+
parent: gos_web_socket.abstract.pusher
218+
class: Gos\Bundle\WebSocketBundle\Pusher\Wamp\WampPusher
219+
arguments:
220+
- @gos_web_socket.push_message.serializer
221+
tags:
222+
- { name: gos_web_socket.pusher, alias: wamp }
223+
216224
gos_web_socket.amqp.server_push_handler:
217225
class: Gos\Bundle\WebSocketBundle\Pusher\Amqp\AmqpServerPushHandler
218226
arguments:

Resources/docs/Pusher.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,27 @@ $pusher->push(['my_data' => 'data'], 'user_notification', ['username' => 'user1'
105105
**NOTE :** `$context` is optional but you can pass option to publish method like 'routing_key', 'publish_flags' and 'attributes' (**See** : https://github.com/pdezwart/php-amqp/blob/master/amqp_exchange.c#L576)
106106
## Websocket Pusher
107107

108-
**NOTE :** Websocket Pusher is not the most faster and powerfull because he have a lot of overhead.
108+
**NOTE :** Websocket Pusher is not the most faster and powerful because he have a lot of overhead (Handshake have high cost).
109+
110+
**NOTE 2 :** He call directly `onPublish` method not `onPush` because we use WAMP protocol.
111+
112+
**1. Bundle Configuration**
113+
114+
```yml
115+
gos_web_socket:
116+
pushers:
117+
wamp:
118+
host: 127.0.0.1
119+
port: 1337
120+
```
121+
122+
**2. Push**
123+
124+
```php
125+
$pusher = $this->container->get('gos_web_socket.wamp.pusher');
126+
//push(data, route_name, route_arguments)
127+
$pusher->push(['my_data' => 'data'], 'user_notification', ['username' => 'user1']);
128+
```
109129

110130
## Rely push to your topic
111131

0 commit comments

Comments
 (0)