我正在开发一个快速聊天应用程序,以提高我的推送技能,并决定开始进入私人频道。我在相同的客户端代码上使用的公共频道应用程序并稍微调整了 App\Events\chatmessagesent 事件(更改了 return new Channel(...) 以返回 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 的更好设置,但我不断从“import Echo from 'laravel-echo'”行中收到错误,说我无法从不支持的模块导入打开,所以我切换回这种形式,因为今天早些时候我让它在公共频道上运行,并且感觉更接近通过这种方法让它运行。欢迎任何建议,谢谢。