Pusher订阅错误。 Laravel

时间:2017-07-21 08:44:42

标签: laravel pusher

我正试着和Pusher在Laravel 5.4中与两个人聊天。

首先,我发送一个Ajax帖子请求:

$('#btn-chat').click(function (){
    $.post('sendMessage',{
        _token: $("[name='csrf-token']").attr('content'),
        user_id: userId,
        chat_id: 1,
        message: $('#btn-input').val()
    },
    function(data) {
        console.log(data);
    });
});

然后,控制器接收请求:

public function sendMessage(Request $request){
    $user = Auth::user();
    $message = new Message;
    $message->fill($request->all());
    $message->message = $request->message;
    $message->save();
    broadcast(new MessageSent($message))->toOthers();
    return 'Return MessageController';
}

此外,这是我的MessageSent事件:

class MessageSent implements ShouldBroadcast {
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct($message) {
        $this->message = $message;
    }

    public function broadcastOn() {
        return new PrivateChannel('chat.'.$this->message->chat_id);
    }
}

我的channels.php文件:

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('chat.*', function ($user, $chat_id) {
    return (int) $user->id === (int) $chat_id;
});

最后,JavaScript会监听事件:

var pusher = new Pusher('pusher_key', {
    cluster: 'us2',
    encrypted: true,
    authEndpoint: 'broadcasting/auth',
    auth: {
        headers: {
        'X-CSRF-Token': $("[name='csrf-token']").attr('content')
        }
    }
});
Pusher.logToConsole = true;
var channel = pusher.subscribe('private-chat.1');
channel.bind('MessageSent', (data) => {
  alert(data.message);
});

应该完成所有工作,但是当我加载聊天页面时,它会在JavaScript控制台上引发错误:

Pusher : No callbacks on private-chat.1 for pusher:subscription_error

2 个答案:

答案 0 :(得分:0)

Pusher在某些情况下自动发出事件。

Pusher : No callbacks on private-chat.1 for pusher:subscription_error

告诉您订阅频道时出错,但您没有将回调绑定到该事件。通过绑定到subscription:error事件,您应该能够获得有关订阅期间遇到的错误的更多信息。您可以找到更多信息here

答案 1 :(得分:0)

正如leesio所说,如果出现错误,Pusher会发出事件。

我是否补充说:

channel.bind('pusher:subscription_error', function(data) {
   console.log(data);
});

它显示在控制台中:

JSON returned from webapp was invalid, yet status code was 200.

因为Laravel广播路由(broadcast / auth)返回了HTTP响应,而不是Pusher所需的JSON。 所以我的解决方案是创建一个新路径(pusherAuth),然后返回Pusher以下JSON:

public function pusherAuth(Request $request){
    $key = 'xxxxxxxxxxxxx';
    $secret = 'xxxxxxxxxxxxxx';
    $channel_name = $request->channel_name;
    $socket_id = $request->socket_id;
    $string_to_sign = $socket_id.':'.$channel_name;
    $signature = hash_hmac('sha256', $string_to_sign, $secret);
    return response()->json(['auth' => $key.':'.$signature]);
}

客户端JavaScript:

var pusher = new Pusher('xxxxxxxxxxxxx', {
    cluster: 'us2',
    encrypted: true,
    authEndpoint: 'pusherAuth', //Before 'broadcasting/auth'
    auth: {
        headers: {
        'X-CSRF-Token': $("[name='csrf-token']").attr('content')
        }
    }
});

那应该做的工作。我不知道为什么我不能用Laravel广播路线做这件事,但如果有人遇到同样的问题,我会发布这个答案。

更多信息: