Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel5.7-Broadcasting-socket.io-redis:在客户端上没有发生任何事情_Php_Laravel 5_Redis_Socket.io_Laravel Echo - Fatal编程技术网

Php Laravel5.7-Broadcasting-socket.io-redis:在客户端上没有发生任何事情

Php Laravel5.7-Broadcasting-socket.io-redis:在客户端上没有发生任何事情,php,laravel-5,redis,socket.io,laravel-echo,Php,Laravel 5,Redis,Socket.io,Laravel Echo,几天以来,我读到了关于如何在本地机器上设置laravel echo的文章。我设法使用redis和socket.io运行laravel echo服务器 问题是事件被触发,但在客户端没有发生任何事情 我知道事件已触发,因为它已记录到我的horizon.log文件中 Horizon started successfully. [2018-08-29 18:35:30][31] Processing: App\Events\NewModel [2018-08-29 18:35:31][31] Proce

几天以来,我读到了关于如何在本地机器上设置laravel echo的文章。我设法使用redis和socket.io运行laravel echo服务器

问题是事件被触发,但在客户端没有发生任何事情

我知道事件已触发,因为它已记录到我的horizon.log文件中

Horizon started successfully.
[2018-08-29 18:35:30][31] Processing: App\Events\NewModel
[2018-08-29 18:35:31][31] Processed:  App\Events\NewModel
然后,在我的echo.log文件中,我可以看到用户连接到通道,echo触发事件

L A R A V E L  E C H O  S E R V E R

version 1.3.9

⚠ Starting server in DEV mode...

✔  Running at localhost on port 60010
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

[6:35:15 PM] - QW3VITRzWPH4FvxPAAAB joined channel: channel-name
Channel: presence-channel-name
Event: App\Events\NewModel
它在端口60010上运行,因为当我尝试在端口6001上运行时,客户端无法连接

我正在windows 10机器上使用virtualbox/homestead

有一个港口正驶入我的家园

ports:
    - send: 60010
      to: 6001
所以,我不明白为什么客户端不记录事件。这就是为什么我现在的代码

echo.js laravel-echo-server.json
{
    "authHost": "https://app.torque.homestead",
    "authEndpoint": "/broadcasting/auth",
    "clients": [{
        "appId": "appId",
        "key": "key"
    }],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "127.0.0.1"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "",
    "port": "60010",
    "protocol": "https",
    "socketio": {},
    "sslCertPath": "app.torque.homestead.crt",
    "sslKeyPath": "app.torque.homestead.key",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}
channels.php 我正在使用的有一些版本:

vagrant@homestead:~$ node -v
v10.8.0
vagrant@homestead:~$ npm -v
6.4.0
vagrant@homestead:~$ php -v
PHP 7.2.9-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Aug 19 2018 07:16:54) ( NTS )
packages.json

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.18",
        "babel-preset-react": "^6.24.1",
        "bootstrap": "^4.1.3",
        "cross-env": "^5.2.0",
        "jquery": "^3.2",
        "laravel-mix": "^2.1.14",
        "lodash": "^4.17.4",
        "popper.js": "^1.14.4",
        "vue": "^2.5.17"
    },
    "dependencies": {
        "@fortawesome/fontawesome": "^1.1.8",
        "@fortawesome/fontawesome-free-brands": "^5.0.13",
        "@fortawesome/fontawesome-free-regular": "^5.0.13",
        "@fortawesome/fontawesome-free-solid": "^5.0.13",
        "@fortawesome/fontawesome-free-webfonts": "^1.0.9",
        "@fortawesome/vue-fontawesome": "0.0.22",
        "ajv": "^6.5.3",
        "bootstrap-confirmation2": "^4.0.1",
        "datatables.net": "^1.10.19",
        "datatables.net-bs4": "^1.10.19",
        "datatables.net-buttons": "^1.5.3",
        "datatables.net-buttons-bs4": "^1.5.3",
        "express": "^4.16.3",
        "ioredis": "^4.0.0",
        "laravel-echo": "^1.4.0",
        "laravel-echo-server": "^1.3.9",
        "socket.io": "^2.1.1",
        "socket.io-client": "^2.1.1",
        "vee-validate": "^2.0.9",
        "vue-inject": "^2.1.1",
        "yarn": "^1.9.4"
    }
}
composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.1.3",
        "aloha/twilio": "^4.0",
        "eyewitness/eye": "dev-beta",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "5.7.*",
        "laravel/horizon": "^1.3",
        "laravel/passport": "^6.0",
        "laravel/tinker": "^1.0",
        "predis/predis": "^1.1"
    },
    "require-dev": {
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
谢谢你帮助我! --编辑-- 在McClayin建议我之后,我寻找redis数据。所以我运行这个:

vagrant@homestead:/var/lib$ redis-cli --scan
presence-channel-name:members
horizon:1
horizon:34
horizon:supervisors
horizon:measured_jobs
horizon:9
horizon:monitor:time-to-clear
horizon:job:App\Jobs\ProcessModels
horizon:35
horizon:queue:default
horizon:job_id
horizon:job:App\Events\NewModel
horizon:masters
horizon:last_snapshot_at
horizon:33
horizon:master:homestead-xVse
horizon:failed_jobs
horizon:32
horizon:supervisor:homestead-xVse:supervisor-1
horizon:recent_jobs
horizon:measured_queues
--编辑2-- 有App\Events\NewModel.php文件

<?php

namespace App\Events;

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 NewModel implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $data;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }

    public function broadcastAs()
    {
        return 'NewModel';
    }
}

尝试将以下内容添加到App\Events\NewModel:

public function broadcastAs()
    
{
        
    return 'NewModel';
    
}
并在客户端的事件名称前添加点符号:

window.Echo.channel('channel-name')
    .listen('.NewModel', (e) => {
        console.log('1:');
        console.log(e);
    })

客户端成功连接到套接字了吗?我想。。。在echo.log中,我可以看到这个
[6:35:15 PM]-QW3VITRzWPH4FvxPAAAB加入的通道:通道名称
。这来自主管处理的my laravel-echo.conf文件句柄。假设客户端加入频道,客户端控制台从echo.js文件接收到连接到socket.io的消息
。您的队列驱动程序是什么?如果您使用redis,我认为您的广播可以在队列中。我的广播和队列都设置为redis
broadcast\u DRIVER=redis
queue\u DRIVER=redis
——我只需查看配置文件,它就设置为使用.env变量。查看redis数据我刚才按照您的建议做了。echo.log和horizon.log显示事件已处理,但客户端未发生任何事件。请尝试将window.echo.channel('channel-name')更改为window.echo.private('channel-name'),并将shouldlbroadcaston()更改为PrivateChannel('channel-name')到NewModel.php中,仍然得到相同的结果。请将NewModel.php添加到问题描述中,好吗?看一下--编辑2--
<?php

namespace App\Events;

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 NewModel implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $data;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }

    public function broadcastAs()
    {
        return 'NewModel';
    }
}
public function broadcastAs()
    
{
        
    return 'NewModel';
    
}
window.Echo.channel('channel-name')
    .listen('.NewModel', (e) => {
        console.log('1:');
        console.log(e);
    })