Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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 laravel websocket客户端无法建立连接,但可以在站点/laravel websocket中建立连接;_Php_Laravel_Websocket - Fatal编程技术网

Php laravel websocket客户端无法建立连接,但可以在站点/laravel websocket中建立连接;

Php laravel websocket客户端无法建立连接,但可以在站点/laravel websocket中建立连接;,php,laravel,websocket,Php,Laravel,Websocket,我正在使用beyondcode/laravel websockets包 以下是我的配置文件 boroadcasting.php: 'connections' => [ 'pusher' => [ 'driver' => 'pusher', 'key' => env('PUSHER_APP_KEY'), 'secret' => env('PUSHER_APP_SECRET'),

我正在使用beyondcode/laravel websockets包

以下是我的配置文件

boroadcasting.php:

'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => true,
                'host' => '127.0.0.1',
                'port' => 6001,
                'scheme' => 'http'
            ],
        ],



websocket.php:
    'apps' => [
        [
            'id' => env('PUSHER_APP_ID'),
            'name' => env('APP_NAME'),
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'path' => env('PUSHER_APP_PATH'),
            'capacity' => null,
            'enable_client_messages' => false,
            'enable_statistics' => true,
        ],
    ],

.env
PUSHER_APP_ID=123456789
PUSHER_APP_KEY=appkey
PUSHER_APP_SECRET=dfghfgjhhjkghkhjdfasrwetrtyuuipjmgh
PUSHER_APP_CLUSTER=mt1
我在app.js中使用以下内容:

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}


import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true,
});
然后,我运行
npm run watch
。 我还没有尝试听任何东西,因为这段代码不能正常工作。并表示无法在控制台中建立连接。这是因为
site/laravel websockets
工作正常,连接没有问题

这是google chrome控制台中显示的错误

app.js?id=4c7e619c826e5fe97c8b:15800 WebSocket connection to 'wss://mysite.test/app/appkey?protocol=7&client=js&version=6.0.3&flash=false' failed: WebSocket is closed before the connection is established.

将其添加到app.js的
echo()下
启用的传输:['ws','wss']

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true,
    enabledTransports: ['ws', 'wss'] // <- add this param
window.Echo=新建Echo({
广播员:“推手”,
键:process.env.MIX\u PUSHER\u APP\u键,
集群:process.env.MIX\u PUSHER\u APP\u集群,
wsHost:window.location.hostname,
wsPort:6001,
disableStats:正确,

EnabledTransport:['ws','wss']/我找到了原因。我在本地计算机上测试应用程序,因此无法使用TLS。echo的默认行为强制TLS。因此,以下配置将修复此问题

如果您使用的是不安全的websocket或在本地集上进行测试
forceTLS:false

    window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true,
    forceTLS: false
});