-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pusher.php
176 lines (144 loc) · 4.17 KB
/
Pusher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace dizews\pushStream;
use Yii;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Utils;
use yii\base\Application;
use yii\base\Component;
use yii\helpers\ArrayHelper;
class Pusher extends Component
{
public $format = 'json';
public $debug = false;
public $eventIdheader = 'X-EventSource-Event';
public $channelSplitter = '/';
/* @var Client */
private $client;
public $serverOptions = [
'host' => 'http://127.0.0.1',
'path' => '/pub'
];
public $listenServerOptions = [
'path' => '/sub',
'modes' => 'stream'
];
public $flushAfterRequest = true;
protected $channels = [];
public function init()
{
if ($this->flushAfterRequest) {
Yii::$app->on(Application::EVENT_AFTER_REQUEST, function () {
$this->flush();
});
}
}
/**
* publish event
*
* @param string $channel channel name
* @param string $event event name
* @param mixed $data body of event
* @return mixed
*/
public function publish(string $channel, string $event, $payload = [])
{
$this->channels[$channel][] = [
'time' => date(\DateTime::RFC7231),
'event' => $event,
'payload' => $payload,
];
return $this;
}
/**
* @param string $channel
*/
public function create(string $channel)
{
$endpoint = $this->makeEndpoint($this->serverOptions);
$this->getClient()->put($endpoint, ['query' => ['id' => $channel]]);
}
/**
* @param string $channel
*/
public function delete(string $channel)
{
$endpoint = $this->makeEndpoint($this->serverOptions);
$this->getClient()->delete($endpoint, ['query' => ['id' => $channel]]);
}
/**
* flush all events onto endpoint
* @return mixed
*/
public function flush()
{
$endpoint = $this->makeEndpoint($this->serverOptions);
Yii::trace('flush events of pusher');
if ($this->channels) {
foreach ($this->channels as $channel => $events) {
foreach ($events as $event) {
//send $payload into $endpoint
$event['channel'] = (string)$channel;
$response = $this->getClient()->post($endpoint, [
'query' => ['id' => $channel],
'headers' => array_filter([
'Content-Type' => $this->format == 'json' ? 'application/json' : null,
$this->eventIdheader => $event['eventId'],
]),
$this->format => $event
]);
}
}
$this->channels = [];
return $response->getBody()->getContents();
}
}
/**
* listen endpoint
*
* @param $channels list of channels
* @param null $callback
*/
public function listen($channels, $callback = null)
{
$endpoint = $this->getListenerHost($channels);
$response = $this->getClient()->get($endpoint, [
'stream' => true
]);
$body = $response->getBody();
while (!$body->eof()) {
if (is_callable($callback)) {
call_user_func($callback, Utils::readline($body));
} else {
echo Utils::readline($body);
}
}
}
public function getListenerHost($channels)
{
$endpoint = $this->makeEndpoint(array_merge($this->serverOptions, $this->listenServerOptions));
if (substr($endpoint, -1) != '/') {
$endpoint .= '/';
}
$endpoint .= implode($this->channelSplitter, (array)$channels);
return $endpoint;
}
/**
* @return Client
*/
protected function getClient()
{
if (!$this->client) {
$this->client = new Client();
}
return $this->client;
}
/**
*
* @param $serverOptions array of server options
* @return string
*/
private function makeEndpoint($serverOptions)
{
return $serverOptions['host'].$serverOptions['path'];
}
}