推送 1.0.0
快速集成推送、即时聊天等功能
该模块基于 webman/push
,用于快速实现推送。
修改 push
模块根目录 push.php
文件 APP-KEY
和 APP-SECRET
,也可以在其他模块根目录添加 push.php
文件。
<?php
return [
'[APP-KEY]' => [
'enable' => true,
'channel_hook' => 'http://127.0.0.1:8787/push/hook/[APP-KEY]',
'app_secret' => '[APP-SECRET]'
]
];
客户端订阅:
<script src="/modules/push/js/push.js"></script>
<script>
var app_key = [APP-KEY];
var connection = new Push({
url: 'wss://www.yoursite.com',
app_key: app_key,
auth: '/push/auth/' + app_key
});
var private_channel = connection.subscribe('private-MESSAGECENTER-' + [ID]);
private_channel.on('message', function(data) {
// do something
});
var public_channel = connection.subscribe('MESSAGECENTER');
public_channel.on('message', function(data) {
// do something
});
</script>
私有频道鉴权:
// 实现PushAuth事件
public function onPushAuth($payload)
{
list($channel_name, $pusher) = $payload->params;
if (get_user()->isLogin() && $channel_name == 'private-MESSAGECENTER-' . get_user()->uid) $payload->result = true;
}
channel_hook:
// 实现PushChannelHook事件
public function onPushChannelHook($payload)
{
var_dump($payload);
}
服务端推送
$pusher = ev('Pusher', [APP-KEY]);
$channel = 'private-MESSAGECENTER-' . $id;
$pusher->trigger($channel, 'message', [
'from' => $from,
'message' => $message
]);