【问题标题】:Internal server on implements ShouldBroadcast实现 ShouldBroadcast 上的内部服务器
【发布时间】:2019-12-04 08:33:33
【问题描述】:

我在 laravel 中做了一个事件来制作 使用 pusher 的 web socket, 在.env 我已经设置了 puser idsecurity,已经在 app.php 中注册,已经在 bootstarp.js 中设置,已经在设置 ​​eventchannel.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;
});

【问题讨论】:

  • 你能发布完整的错误和堆栈跟踪吗?

标签: laravel pusher


【解决方案1】:

如果你正在使用组件,那么你应该npm run dev 首先尝试使用“频道”,如果它的工作,然后使用私人频道编辑 App\provider\BroadcastServiceProvider

public function boot()
{
    Broadcast::routes(['middleware' => ['auth:api']]);

    require base_path('routes/channels.php');
}

【讨论】:

  • 哦,它正在使用 web 替换 auth:api.. 非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 2019-11-16
  • 2014-07-08
相关资源
最近更新 更多