推式客户端/ pusher / auth返回错误500(内部服务器错误)

时间:2020-01-28 22:03:52

标签: php jquery ajax laravel pusher

我正在开发一个快速聊天应用程序,以利用Pusher开发我的技能,并决定开始进入私人频道。我在相同的客户端代码上有一个公共频道应用程序,并略微调整了App \ Events \ chatmessagesent事件(将return new Channel(...)更改为return new PrivateChannel(...))正在返回一个特殊的错误。当我加载聊天页面时,即使我只是调整了代码以指向私有服务器,现在在尝试发布到pusher / auth web.php路由时也出现了错误500。我希望我在调试方面有更多的东西,所以我可以帮助缩小原因的范围,但是到目前为止,我对调试它的运气还不如通常的调试方式好。我将从每个相关文件中发布代码以供参考。

// Web.php

Route::post('/pusher/auth', function(){
        return true;
});

// Event File (in App\Events)
...
public function broadcastOn()
    {
        return new PrivateChannel('chatroom.' . $this->chatroomID);
    }
...

// Client-side Code

@extends('layouts.mainpage2')

@section('headincs')
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Chatroom</title>
    <script>
    window.Laravel = {!! json_encode([
        'chatroom' => $returndata['chatroom']->id,
    ]) !!};
</script>
@endsection

@section('content')
    <style>
        .vh-50 {
            height: 50vh !important;
        }
    </style>
    <script src="https://js.pusher.com/5.0/pusher.min.js"></script>
    <script>

        var pusher = new Pusher('pusherkey', {
            cluster: 'us2',
            forceTLS: true, 
            authEndpoint: '/pusher/auth',
            auth: {
                headers: {
                    'X-CSRF-Token': "{{ csrf_token() }}"
                }
            }
        });

    var channel = pusher.subscribe('private-chatroom.' + window.Laravel.chatroom);
    channel.bind('chatmessagesent', function(data) {
      alert(JSON.stringify(data));
      console.log("received message");
    });

    $(document).ready(function() {
        $('#chatMessageForm').submit(function(event){
            event.preventDefault();
            $.ajax({
                type: 'POST',
                url: '/chatroom/' + window.Laravel.chatroom + '/sendmsg',
                data: $('form#chatMessageForm').serialize(),
                dataType: 'json',
            })
            $('#msgContent').val("");


            return false;
        });
    });
  </script>
    <a href="/userlanding" class="pl-3 pt-3">Back to dashboard</a>
    <div class="container my-5 border border-primary rounded">
        <h3 class="text-center pt-3">{{$returndata['chatroom']->chatroom_name}}</h3>
        <h6 class="text-center pt-2 font-weight-light">Last active at: {{$returndata['chatroom']->last_active_at}}</h6>
        <div class="container my-3 border vh-50">
            <div class="d-flex flex-column-reverse">
            @foreach($returndata['relmsgs'] as $message)
                <div class="w-50">
                    Message Data
                </div>
            @endforeach
            </div>
        </div>
        <div class="row mx-0">
            {!! Form::open(['route' => ['chatroom.sendmessage', 'chatroomID'=>$returndata['chatroom']->id], 'method' => 'POST', 'class' => 'w-100 row mx-0 justify-content-center mb-4', 'id' => 'chatMessageForm']) !!}
                @csrf
                <div class="col-md-10">
                    <textarea class="form-control w-100" name="msgContent" id="msgContent" rows="3"></textarea>
                </div>
                <div class="col-md-2">
                    {{Form::submit('Send Message', ['class' => 'btn btn-primary w-100 h-100'])}}
                </div>
            {!! Form::close() !!}
        </div>
    </div>
@endsection

// Bootstrap.js (in Resources\js)

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
     broadcaster: 'pusher',
     key: 'pusherkey',
     cluster: 'us2',
     forceTLS: true
});

bootstrap.js的最后一行(已加载到页面上)显然是为Echo设置的,因此,我将其包括在内以防可能受到干扰。我尝试切换到Echo,以为Pusher对于Laravel Echo可能是更好的设置,但是我一直从“从'laravel-echo'导入Echo”行中遇到错误,说我无法从一个不是是开放的,所以我切换回此表单,因为今天早些时候我在公共频道上使用了该表单,因此感觉更接近通过此方法进行工作。欢迎提出任何建议,谢谢。

1 个答案:

答案 0 :(得分:0)

在这里查看我到目前为止所做的工作:What can I do to resolve this pusher error-JSON returned from auth endpoint was invalid, yet status code was 200?。虽然我还没有完全解决这个问题,但通过遵循您对“/pusher/auth”的路由定义,我走得更近了。我不再收到无效的 json 响应,我现在收到 200 响应代码。我现在必须弄清楚的是新错误:“订阅 private-user.3 所需的身份验证信息”。