大大們!新年好!
我的 laravel 項目,裡面使用了2 websockets 服務,
1台(pusher)是配合Laravel廣播,使用 package架設的。
另1台(自架)用在聊天室,下載 GatewayWorker所架設。
狀況如下所述:
i58a.com 是我的開發機器,部屬在辦公室,運作一切正常。
i58d.com 是正式機器,完全相同的 code 部屬到 AWS,卻出現無法連線問題(如 F12 控制台所示)。
在辦公室的win10使用xampp架站,啟動xampp以後,還需要執行2個動作
1、php artisan websockets:serve --port=99 ---這會使 pusher 正常跑
2、執行 我1個 .cmd 檔案,使 GatewayWorker 的所有服務執行,聊天室就正常連線
我把專案 push 到 github
而我部屬到AWS EC2的主機是 ubuntu20.04 安裝好 apache mysql php 版本都跟 win10相同
我SSH到EC2主機,然後 git clone 後執行如下:
基本動作省略
composer install
npm install
npm run prod
然後一樣執行2個動作
1、php artisan websockets:serve --port=99 ---沒有使 pusher 正常跑
2、執行 1個 php start.php start 檔案,使 GatewayWorker 的所有服務執行,但是聊天室也是無法正常連線
真不知是何原因?
PS:因為我的程式在執行登入時,有用到使用 websocket 的 pucher 服務,如果我執行帳號登入作業,在 storage/logs/laravel.log 會出現錯誤訊息如下:
[2022-02-10 13:22:42] local.ERROR: Failed to connect to Pusher. {"userId":3,"exception":"[object] (Illuminate\\Broadcasting\\BroadcastException(code: 0): Failed to connect to Pusher. at /var/www/i***d/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:121)
[stacktrace]
#0 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php(64): Illuminate\\Broadcasting\\Broadcasters\\PusherBroadcaster->broadcast()
#1 /var/www/i***8d/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Broadcasting\\BroadcastEvent->handle()
#2 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Container/Util.php(37): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#3 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#4 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#5 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Container/Container.php(590): Illuminate\\Container\\BoundMethod::call()
#6 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\\Container\\Container->call()
#7 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}()
#8 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#9 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\\Pipeline\\Pipeline->then()
#10 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php(113): Illuminate\\Bus\\Dispatcher->dispatchNow()
#11 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(289): Illuminate\\Broadcasting\\BroadcastManager->queue()
#12 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(212): Illuminate\\Events\\Dispatcher->broadcastEvent()
#13 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php(57): Illuminate\\Events\\Dispatcher->dispatch()
#14 /var/www/i***d/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(219): Illuminate\\Broadcasting\\PendingBroadcast->__destruct()
#15 /var/www/i58d/app/Http/Controllers/Auth/LoginController.php(104): broadcast()
PS:websocket 的設定 包含證書...等都仔細確認過沒問題,也比對過 開發機器的設定,應該不是我設定的問題
這樣的情況可有解方?期待有大大可以解救一下,在此先行拜謝。
我共使用了 2台的 websocket補充一下我的配置:
(關於pusher 廣播使用的 websocket)
1、websockets.php
<?php
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
return [
'dashboard' => [
'port' => env('LARAVEL_WEBSOCKETS_PORT', 9***9),
],
'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,
],
],
BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,
'allowed_origins' => [
//
],
'max_request_size_in_kb' => 250,
'path' => 'laravel-websockets',
'middleware' => [
'web',
Authorize::class,
],
'statistics' => [
'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,
'logger' => BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger::class,
'interval_in_seconds' => 60,
'perform_dns_lookup' => false,
],
'ssl' => [
'local_cert' => "/etc/letsencrypt/live/i***d.com/fullchain.pem",
'local_pk' => "/etc/letsencrypt/live/i***d.com/privkey.pem",
'passphrase' => null,
'verify_peer' => false,
],
'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];
2、broadcasting.php
<?php
return [
'default' => env('BROADCAST_DRIVER', 'null'),
'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'),
'host' => '127.0.0.1',
'port' => 9***9,
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
],
// 'useTLS' => true,
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
3、bootstrap.js
window._ = require('lodash');
try {
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
wsHost: window.location.hostname,
wsPort: 9***9,
wssPort: 9***9,
disableStats: true,
forceTLS: true,
});
(關於聊天室使用的 websocket)
1、start_businessworker.php
<?php
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:5***1';
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
2、start_register.php
<?php
use \Workerman\Worker;
use \GatewayWorker\Register;
// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';
$register = new Register('text://0.0.0.0:5***1');
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
3、start_gateway.php
<?php
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';
$context = array(
'ssl' => array(
'local_cert' => '/etc/letsencrypt/live/i***d.com/fullchain.pem',
'local_pk' => '/etc/letsencrypt/live/i***d.com/privkey.pem',
'verify_peer' => false,
)
);
$gateway = new Gateway("websocket://0.0.0.0:9***5", $context);
$gateway->transport = 'ssl';
$gateway->name = 'i***d';
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 4001;
$gateway->registerAddress = '127.0.0.1:5***1';
$gateway->pingInterval = 60;
$gateway->pingData = '{"type":"ping"}';
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
4、在模板調用方式
var wss = new WebSocket("wss://i***d.com:9***5");