laravel使用广播laravel-echo配置后不能在控制台显示,也没有报错

梨花猫
  • 96

1.我在用laravle的laravle-echo配置的时候完的时候没有在console中看到输出的内容
-.下面的是我的我写的事件的代码

<?php

namespace App\Events;

use App\UserInput;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class UserInputSaveEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $body;

    public function __construct(UserInput $input)
    {
        return $this->body = $input;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('server.created');
        // return new PrivateChannel('user.' . $this->body->id);
    }

    public function broadcastAs()
    {
        return 'server.created';
    }
    public function broadcastWith()
    {
        return ['user_id' => $this->body];
    }
}

路由的配置文件信息

Route::get('/show', function() {
    return view('show');
});

Route::post('userInput', 'HomeController@userinput');

这个是我的laravel-echo-server.json的配置文件

{
    "authHost": "http://localhost",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

这是我的env的配置文件

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

我在控制台运行laravel-echo-serverd的控制台的提示信息的

clipboard.png

我运行redis服务器的时候控制的提示信息是

clipboard.png

在的我show.blade.php中的代码是

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Document</title>
</head>
<body>
<script src="{{asset('js/app.js')}}" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    //    var socket = io('http://localhost:3000');
//    var socketId = Echo.socketId();
    Echo.channel('server.created').listen('server.created', (e) => {
        console.log('weqweq');
    });
</script>
</body>
</html>

app.js中的文件信息是
import Echo from "laravel-echo";

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host       : 'http://localhost:6001'
});

我不知道我哪里配置错了,希望大神们可以告诉我一下

回复
阅读 6.7k
5 个回答

已经解决了
这个问题是作用域错误了。。。。。。无语

我也出现这个问题,就是redis和laravel-echo-server没有反应,你是怎么解决的?求解

毛瞌羊12_34
  • 2
新手上路,请多包涵

我npm完laravel-echo-server 在输入 laravel-echo-server init报command not found

有人解决了吗

前台页面没有和后台的channel通道沟通

宣传栏