首页 > 解决方案 > Websocket未连接 - Docker

问题描述

我让 Laravel 在 Docker 环境中运行,包 mysql、nginx 和 php-fpm。我将它用作 API。我想添加 websocket,服务器是通过 php artisan websocket:serve 使用 supervisor 启动的。我无法连接到统计 laravel-websockets,我在控制台上看不到任何乒乓球。

我究竟做错了什么 ?

广播.php

   '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'),
            'scheme' => 'http',
            'host' => '127.0.0.1',
            'port' => 6001
        ],
    ],

ngnix.conf

map $http_upgrade $type {
default "web";
websocket "ws";
 }

server {
 listen 80 ;
 listen 444 ssl ;

client_max_body_size 520M;

access_log /var/log/nginx/application.access.log;
# mapping the entry point
root /application/Edidact_Backend/public;

# Deny all . files
location / {
    try_files $uri /index.php$is_args$args;
}
location @web  {
    try_files $uri $uri/ /index.php?$query_string;
  }
location @ws  {
  proxy_pass             http://php-fpm:6001;
  proxy_set_header Host  $host;
  proxy_read_timeout     60;
  proxy_connect_timeout  60;
  proxy_redirect         off;

# Allow the use of websockets
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}
# Handle Cache Files Automatically
location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico)$ {
    expires 30d;
    add_header Cache-Control "max-age=0, must-revalidate";
}
# Handleing Php Files
location ~ ^/index\.php(/|$) {
    fastcgi_pass php-fpm:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    fastcgi_index app.php;
    send_timeout 1800;
    fastcgi_read_timeout 1800;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    include fastcgi_params;
}

location ~ \.php$ {
    return 404;
}
# Statics
location /(bundles|media) {
    access_log off;
    expires 30d;
    try_files $uri @rewriteapp;

}

}

websockets.php

'apps' => [
    [
        'id' => env('PUSHER_APP_ID'),
        'name' => env('APP_NAME'),
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'enable_client_messages' => false,
        'enable_statistics' => true,
    ],
]

容器

在此处输入图像描述

标签: laraveldockerwebsocketlaravel-websockets

解决方案


据我所知,您的 websocket 服务器与您的 php 服务器不在同一个容器中,因此当您尝试连接到 localhost 时它会失败。

如果您想连接到不同的容器,您应该使用该容器的名称而不是 localhost,在您的情况下:

'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'),
        'scheme' => 'http',
        'host' => 'php-fpm-service-V2', // Name of the php server
        'port' => 6001
    ],
],

推荐阅读