use Webman\Push\Api;
$api = new Api(
// webman下可以直接使用config獲取配置,非webman環(huán)境需要手動寫入相應配置
'http://127.0.0.1:3232',
config('plugin.webman.push.app.app_key'),
config('plugin.webman.push.app.app_secret')
);
$userId=1;
// 給訂閱 user-1 的所有客戶端推送 message 事件的消息
$api->trigger('private-user-'.$userId, 'message', [
'from_uid' => 2,
'content' => '你好,這個是消息內(nèi)容'
]);
$userId
在不寫死的情況下,如何監(jiān)聽或者獲取到當前訂閱的$userId
,并根據(jù)當前訂閱$userId
,單獨推送消息呢?
比如這段代碼放控制器里,然后客戶端傳遞要發(fā)送的userid
我一直是在process目錄下定義一個類,通過Timer進行推送。按您這樣說,那么我可否在push中auth認證的控制器中進行推送?
public function getUserPushAuth(Request $request):Response
{
$uid = JwtToken::getCurrentId();
if($uid){
$pusher = new Api(str_replace('0.0.0.0', '127.0.0.1', config('plugin.webman.push.app.api')), config('plugin.webman.push.app.app_key'), config('plugin.webman.push.app.app_secret'));
$channel_name = $request->post('channel_name');
$api = new Api(
'http://127.0.0.1:3232',
config('plugin.webman.push.app.app_key'),
config('plugin.webman.push.app.app_secret')
);
//推送K線
Timer::add(1, function () use ($api,$uid) {
// var_dump($uid);
$api->trigger('private-user-'.$uid,'message', time());
});
return response($pusher->socketAuth($channel_name, $request->post('socket_id')));
}
return response('Forbidden', 403);
}
那總要一個觸發(fā)的契機吧?
var user_channel = connection.subscribe('private-user-1');
user_channel.on('client-message', function (data) {
//
});
user_channel.trigger('client-message', {form_uid:2, content:"hello"});
這樣,如何去定位控制器?
使用Timer一直推送數(shù)據(jù),那么每個人訂閱私有頻道的人,或者取消訂閱后。會不會導致內(nèi)存累積。如果會,在webman/push中應該怎么刪除timer