Skip to content

Commit 1ffef26

Browse files
committed
add _subscribeRaw方法
1 parent 8e63206 commit 1ffef26

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,10 @@ https://pusher.com/docs/channels/channels_libraries/libraries/
524524
在一些服务器监控场景下,我们需要获取全量的往来信息,包括客户端的消息和服务端的回执等
525525

526526
- 创建一个中间件服务类,use引入ChannelMethods
527+
- 客户端与服务端的任何通讯消息会触达`_subscribeResponse`方法,请在`_subscribeResponse`方法中实现对应业务逻辑,入日志等;
527528

529+
- `_subscribeResponse`方法是经过业务处理后的方法,如果想要订阅原始数据,请实现`_subscribeRaw`方法
530+
528531
```php
529532
<?php declare(strict_types=1);
530533

@@ -539,13 +542,17 @@ class PushServerMiddleware
539542
/** @inheritDoc */
540543
public static function _subscribeResponse(string $type, array $data): void
541544
{
542-
// TODO
545+
// TODO 业务类型中间件
546+
}
547+
548+
/** @inheritDoc */
549+
public static function _subscribeRaw($channel, $raw): void
550+
{
551+
// TODO 订阅通道原始数据
543552
}
544553
}
545554
```
546555

547-
- 客户端与服务端的任何通讯消息会触达_subscribeResponse方法,请在_subscribeResponse方法中实现对应业务逻辑,入日志等;
548-
549556
- 在项目config/process.php或config/plugin/workbunny/webman-push-server/process.php中添加配置
550557

551558
```php
@@ -561,8 +568,10 @@ class PushServerMiddleware
561568
#### Tips:
562569

563570
- 中间件切记保持单进程运行,本质上是与push-server进程组监听同一个内部通讯通道
564-
- _subscribeResponse方法中请勿执行耗时操作,否则将影响性能,建议异步执行,如投送到队列进行消费
565-
- _subscribeResponse中type为client时为客户端消息,type为server时为服务端回执消息,其他则详见[AbstractPublishType.php](src/PublishTypes/AbstractPublishType.php)
571+
- `_subscribeResponse`方法中请勿执行耗时操作,否则将影响性能,建议异步执行,如投送到队列进行消费
572+
- `_subscribeResponse``type``client`时为客户端消息,`type``server`时为服务端回执消息,其他则详见[AbstractPublishType.php](src/PublishTypes/AbstractPublishType.php)
573+
- `_subscribeRaw`方法中请勿执行耗时操作,否则将影响性能,建议异步执行,如投送到队列进行消费
574+
- `_subscribeRaw``channel`为订阅的通道名,`raw`为原始数据,通常为json字符串
566575
- 该中间件更适合作为监控服务或者日志服务,如果作为拦截器等服务,可能存在调用链路较长的问题
567576
- 样例查看,[PushServerMiddleware.php](tests/Examples/PushServerMiddleware.php)
568577

src/Traits/ChannelMethods.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Workerman\Redis\Client;
1414
use Workerman\Timer;
1515

16+
/**
17+
* @method static _subscribeRaw($channel, $raw) 订阅原始消息
18+
*/
1619
trait ChannelMethods
1720
{
1821
/** @var Client[] */
@@ -140,18 +143,21 @@ public static function publishUseRetry(string $type, array $data, float $retryIn
140143
* 订阅回调
141144
*
142145
* @param $channel
143-
* @param $message
146+
* @param $raw
144147
* @return void
145148
*/
146-
public static function _onSubscribe($channel, $message): void
149+
public static function _onSubscribe($channel, $raw): void
147150
{
148-
$message = @json_decode($message, true);
151+
if (is_callable([static::class, '_subscribeRaw'])) {
152+
static::_subscribeRaw($channel, $raw);
153+
}
154+
$message = @json_decode($raw, true);
149155
if (
150156
($type = $message['type'] ?? null) and
151157
($data = $message['data'] ?? null)
152158
) {
153159
// 订阅响应
154-
static::_subscribeResponse($type, $data);
160+
static::_subscribeResponse($type, $data, $raw);
155161
} else {
156162
Log::channel('plugin.workbunny.webman-push-server.notice')->notice(
157163
"[Channel] $channel -> $message format error. "
@@ -163,7 +169,7 @@ public static function _onSubscribe($channel, $message): void
163169
* 订阅响应
164170
*
165171
* @param string $type 消息类型
166-
* @param array $data 消息数据
172+
* @param array $data 解析后的消息数据
167173
* @return void
168174
*/
169175
abstract public static function _subscribeResponse(string $type, array $data): void;

0 commit comments

Comments
 (0)