forked from dizews/yii2-push-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPusher.php
147 lines (120 loc) · 3.6 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
<?php
namespace dizews\pushStream;
use Yii;
use GuzzleHttp\Client;
use GuzzleHttp\Stream\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 = 'Event-Id';
/* @var Client */
private $client;
public $serverOptions = [
'useSsl' => false,
'host' => '127.0.0.1',
'port' => 80,
'path' => '/pub'
];
public $listenServerOptions = [
'path' => '/sub',
'modes' => 'stream'
];
public $flushAfterRequest = true;
protected $channels = [];
public function init()
{
$this->listenServerOptions = ArrayHelper::merge($this->serverOptions, $this->listenServerOptions);
$this->client = new Client();
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($channel, $event, $data)
{
$this->channels[$channel][] = [
'name' => $event,
'body' => $data
];
if (!$this->flushAfterRequest) {
return $this->flush();
}
return true;
}
/**
* 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
$response = $this->client->post($endpoint, [
'headers' => [
'Content-Type' => $this->format == 'json' ? 'application/json' : null,
$this->eventIdheader => $event['name'],
],
'debug' => $this->debug,
'query' => ['id' => $channel],
$this->format => $event['body']
]);
}
}
$this->channels = [];
return $response->getBody()->getContents();
}
}
/**
* listen endpoint
*
* @param $channels list of channels
* @param null $callback
*/
public function listen($channels, $callback = null)
{
$endpoint = $this->makeEndpoint($this->listenServerOptions);
if (substr($this->listenServerOptions['path'], -1) != '/') {
$endpoint .= '/';
}
$endpoint .= implode(',', (array)$channels);
$response = $this->client->get($endpoint, [
'debug' => $this->debug,
'stream' => true
]);
$body = $response->getBody();
while (!$body->eof()) {
if (is_callable($callback)) {
call_user_func($callback, Utils::readline($body));
} else {
echo Utils::readline($body);
}
}
}
/**
*
* @param $serverOptions array of server options
* @return string
*/
private function makeEndpoint($serverOptions)
{
$protocol = $serverOptions['useSsl'] ? 'https' : 'http';
return $protocol .'://'. $serverOptions['host'].':'.$serverOptions['port'].$serverOptions['path'];
}
}