【问题标题】:Laravel Pusher private channel not working with JqueryLaravel Pusher 私人频道不适用于 Jquery
【发布时间】: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 中

标签: jquery laravel pusher


【解决方案1】:

在私有频道的推送文档中,名称必须以 private-like 为前缀

var channel = pusher.subscribe('private-test-channel');
channel.bind('App\\Events\\NewMessage', function(data) {

您可以找到文档here

【讨论】:

  • 即使使用私有前缀也无法正常工作。
  • 尽管它调试到控制台但没有绑定或订阅。
  • 你也改变了 channels.php 吗?
  • 不,和之前一样。
猜你喜欢
  • 2019-06-04
  • 2018-02-23
  • 2017-05-12
  • 2017-11-27
  • 2014-07-15
  • 2020-03-24
  • 2016-02-24
  • 2016-11-06
  • 1970-01-01
相关资源
最近更新 更多