【发布时间】:2021-06-17 17:33:23
【问题描述】:
当我使用公共频道时,推送器成功发送数据。但是,一旦我在 channels.php 文件中使用具有正确路由的私人频道,它就不起作用了。
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['test-channel'];
}
When i use **return new PrivateChannel('test-channel');** in broadcastOn() method it won't work. This is my channels.api file code:
<?php
use Illuminate\Support\Facades\Broadcast;
use App\Models\Message;
use Auth;
/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('test-channel', function ($user) {
return true;
});
这是我使用 jquery 加载推送消息的地方。
function pushnote()
{ Pusher.logToConsole = true;
var pusher = new Pusher('{{env("MIX_PUSHER_APP_KEY")}}', {
cluster: '{{env("PUSHER_APP_CLUSTER")}}',
encrypted: true,
});
var channel = pusher.subscribe('test-channel');
channel.bind('App\\Events\\NewMessage', function(data) {
// console.log(data.message.content);
// alert(data.message.content);
// alert(data);
alert('success');
// getLatestMessage(data.message.content);
getLatestMessage(data);
});
}
我想在我的聊天应用程序中使用推送器功能。请有人帮忙。您可以私下联系我。
【问题讨论】:
-
什么'不起作用'?服务器发送事件还是客户端?客户端订阅频道好吗?
-
不订阅私人频道。但它会调试到推送控制台。
-
我不确定这意味着什么 - 你能分享在推送控制台中找到的内容吗?
-
我在推送调试控制台中收到此响应。
-
频道:private-test-channel,事件:App\Events\NewMessage,此消息在 json 中