【发布时间】:2020-05-14 09:35:07
【问题描述】:
我正在开发一个快速聊天应用程序来提高我的推送技能,并决定开始进入私人频道。我在相同的客户端代码上使用的公共频道应用程序并稍微调整了 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”行中收到错误,说我无法从不是的模块导入开放,所以我切换回这个表格,因为我今天早些时候在公共频道上工作,感觉更接近通过这种方法让它工作。欢迎提出任何建议,谢谢。
【问题讨论】:
-
开启所有错误检查并检查您的日志文件。
-
我应该添加哪些行作为错误检查?即使在 500 错误之后,我仍然连接到 websocket,因为键入和提交消息仍然会向推送器上的错误调试页面提交一个事件,只是该事件没有被任何其他客户端接收,因为它不是频道的授权成员。
-
将此添加到您的 php 文件的顶部,error_reporting(E_ALL);运行脚本并检查你的错误日志,它会显示所有的 indo。
-
在您的帮助下得到了这条日志记录; “local.ERROR:响应内容必须是实现__toString()的字符串或对象,给出“布尔”。{“userId”:5,“异常”:“[object](UnexpectedValueException(代码:0):”跨度>
标签: php jquery ajax laravel pusher