【发布时间】:2019-12-04 08:33:33
【问题描述】:
我在 laravel 中做了一个事件来制作 使用 pusher 的 web socket,
在.env 我已经设置了 puser id 和 security,已经在 app.php 中注册,已经在 bootstarp.js 中设置,已经在设置 event和channel.php,但是当我实现 ShouldBroadcast 时,会出现内部错误
这里是事件代码:
namespace App\Events;
use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewMessage implements ShouldBroadcast //jika di nyalakan internal error
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('messages.'.$this->message->to);
}
public function broadcastWith(){
return ['message'=>$this.message];
}
}
这是控制台日志中的错误
app.js:279 POST http://localhost:8000/conversation/send 500 (Internal Server Error)
dispatchXhrRequest @ app.js:279
xhrAdapter @ app.js:118
dispatchRequest @ app.js:726
Promise.then (async)
request @ app.js:528
Axios.<computed> @ app.js:553
wrap @ app.js:1071
sendMessage @ app.js:1998
invokeWithErrorHandling @ app.js:50394
invoker @ app.js:50719
invokeWithErrorHandling @ app.js:50394
Vue.$emit @ app.js:52414
send @ app.js:2044
keydown @ app.js:48351
invokeWithErrorHandling @ app.js:50394
invoker @ app.js:50719
original._wrapper @ app.js:56072
app.js:653 Uncaught (in promise) Error: Request failed with status code 500
at createError (app.js:653)
at settle (app.js:899)
at XMLHttpRequest.handleLoad (app.js:166)
和channels.php
Broadcast::channel('messages.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
【问题讨论】:
-
你能发布完整的错误和堆栈跟踪吗?