forked from yiicod/yii2-socketio
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBroadcast.php
executable file
·189 lines (164 loc) · 4.87 KB
/
Broadcast.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
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace hyperia\socketio;
use Exception;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\HtmlPurifier;
use yii\helpers\Json;
use yiicod\base\helpers\LoggerMessage;
use hyperia\socketio\drivers\RedisDriver;
use hyperia\socketio\events\EventPolicyInterface;
use hyperia\socketio\events\EventPubInterface;
use hyperia\socketio\events\EventRoomInterface;
use hyperia\socketio\events\EventSubInterface;
/**
* Class Broadcast
*
* @package hyperia\socketio
*/
class Broadcast
{
protected static $channels = [];
/**
* Subscribe to event from client
*
* @param string $event
* @param array $data
*
* @throws Exception
*/
public static function on(string $event, array $data)
{
// Clear data
array_walk_recursive($data, function (&$item, $key) {
$item = HtmlPurifier::process($item);
});
Yii::info(Json::encode([
'type' => 'on',
'name' => $event,
'data' => $data,
]), 'socket.io');
$eventClassName = self::getManager()->getList()[$event] ?? null;
if (null === $eventClassName) {
Yii::error(LoggerMessage::trace("Can not find $event", Json::encode($data)));
}
Yii::$container->get(Process::class)->run($eventClassName, $data);
}
/**
* Handle process from client
*
* @param string $handler
* @param array $data
*/
public static function process(string $handler, array $data)
{
try {
/** @var EventSubInterface|EventPolicyInterface $event */
$event = new $handler($data);
if (false === $event instanceof EventSubInterface) {
throw new Exception('Event should implement EventSubInterface');
}
Yii::$app->db->close();
Yii::$app->db->open();
if (true === $event instanceof EventPolicyInterface && false === $event->can($data)) {
return;
}
$event->handle($data);
} catch (Exception $e) {
Yii::error(LoggerMessage::log($e, Json::encode($data)));
}
}
/**
* Emit event to client
*
* @param string $event
* @param array $data
*
* @throws Exception
*/
public static function emit(string $event, array $data)
{
$eventClassName = self::getManager()->getList()[$event] ?? null;
try {
if (null === $eventClassName) {
throw new Exception("Can not find $event");
}
/** @var EventPubInterface|EventRoomInterface $event */
$event = new $eventClassName($data);
if (false === $event instanceof EventPubInterface) {
throw new Exception('Event should implement EventPubInterface');
}
$data = $event->fire($data);
if (true === $event instanceof EventRoomInterface) {
$data['room'] = $event->room();
}
Yii::info(Json::encode([
'type' => 'emit',
'name' => $event,
'data' => $data,
]), 'socket.io');
foreach ($eventClassName::broadcastOn() as $channel) {
static::publish(static::channelName($channel), [
'name' => $eventClassName::name(),
'data' => $data,
]);
}
} catch (Exception $e) {
Yii::error(LoggerMessage::log($e));
}
}
/**
* Prepare channel name
*
* @param $name
*
* @return string
*/
public static function channelName($name)
{
return $name . self::getManager()->nsp;
}
/**
* Publish data to redis channel
*
* @param string $channel
* @param array $data
*/
public static function publish(string $channel, array $data)
{
static::getDriver()->getConnection(true)->publish($channel, Json::encode($data));
}
/**
* Redis channels names
*
* @return array
*/
public static function channels(): array
{
if (empty(self::$channels)) {
foreach (self::getManager()->getList() as $eventClassName) {
self::$channels = ArrayHelper::merge(self::$channels, $eventClassName::broadcastOn());
}
self::$channels = array_unique(self::$channels);
self::$channels = array_map(function ($channel) {
return static::channelName($channel);
}, self::$channels);
//Yii::info(Json::encode(self::$channels));
}
return self::$channels;
}
/**
* @return RedisDriver
*/
public static function getDriver()
{
return Yii::$app->broadcastDriver;
}
/**
* @return EventManager
*/
public static function getManager()
{
return Yii::$app->broadcastEvents;
}
}