问题
I'm working on developing a quick chat application to develop my skills with pusher and decided to start getting into private channels. The public channel application I had on the same client-side code and slightly tweaked App\Events\chatmessagesent event (changed return new Channel(...) to return new PrivateChannel(...)) is returning a peculiar error. When I load up the chat page, even though I just tweaked the code to point to a private server, I now get a Error 500 when trying to post to the pusher/auth web.php route. I wish I had more as far as debugging was concerned so I could help narrow down the reasons as to why this might be the case but so far I haven't had any luck with debugging it in the way I would normally debug something. I'll post the code from every relevant file for reference.
// 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
});
The final line from the bootstrap.js, which is loaded onto the page, is obviously setup for Echo, so I just included it in case there might be some interference. I tried switching over to Echo, thinking perhaps Pusher is better setup for Laravel Echo but I kept getting an error from the line "import Echo from 'laravel-echo'", saying that I couldn't import from a module that wasn't open, so I switched back to this form because I had it working on a public channel earlier today and felt closer to getting it working through this method. Open to any suggestions, thank you.
来源:https://stackoverflow.com/questions/59957745/pusher-client-side-pusher-auth-returning-error-500-internal-server-error