gateway中的websocket配置wss域名,总是无法连接,ip可以,换成域名就不行

znbiz

start_gateway.php
<?php
/**

// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';

// 证书最好是申请的证书
$context = array(
// 更多ssl选项请参考手册 https://php.net/manual/zh/context.ssl.php
'ssl' => array(
// 请使用绝对路径
'local_cert' => 'D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/ssl/mytestworkhaha.com.pem', // 也可以是crt文件
'local_pk' => 'D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/ssl/mytestworkhaha.com.key',
'verify_peer' => false,
// 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
)
);
// gateway 进程,这里使用Text协议,可以用telnet测试

$gateway = new Gateway("websocket://0.0.0.0:8282", $context);

$gateway = new Gateway("websocket://127.0.0.1:8282", $context);
// 3. 指定使用 ssl 传输层协议
$gateway->transport = 'ssl';
// gateway名称,status方便查看
$gateway->name = 'YourAppGateway';
// gateway进程数,一般设置2个就足够
$gateway->count = 2;
// 本机ip,分布式部署时使用内网ip
$gateway->lanIp = '127.0.0.1';
// 内部通讯起始端口,假如$gateway->count=2,起始端口为2900
// 则一般会使用2900 2901 2个端口作为内部通讯端口
$gateway->startPort = 2900;
// 服务注册地址
$gateway->registerAddress = '127.0.0.1:1236';

// 心跳间隔
//$gateway->pingInterval = 10;
// 心跳数据
//$gateway->pingData = '{"type":"ping"}';

/
// 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
$gateway->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
// 可以在这里判断连接来源是否合法,不合法就关掉连接
// $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
{
$connection->close();
}
// onWebSocketConnect 里面$_GET $_SERVER是可用的
// var_dump($_GET, $_SERVER);
};
};
/

// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}

nginx.conf:
server {
listen 443 ssl;
ssl_certificate D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/ssl/mytestworkhaha.com.pem;
ssl_certificate_key D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/ssl/mytestworkhaha.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
server_name mytestworkhaha.com;
root "D:/phpstudy_pro/WWW/GatewayWorker/GatewayWorker";

    location /ws {
        proxy_pass http://127.0.0.1:8282/;  # Workerman监听地址
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;  # 处理 WebSocket 升级
        proxy_set_header Connection 'Upgrade';  # 处理 WebSocket 升级

    }

    location / {
        index index.php index.html error/index.html;
        error_page 400 /error/400.html;
        error_page 403 /error/403.html;
        error_page 404 /error/404.html;
        error_page 500 /error/500.html;
        error_page 501 /error/501.html;
        error_page 502 /error/502.html;
        error_page 503 /error/503.html;
        error_page 504 /error/504.html;
        error_page 505 /error/505.html;
        error_page 506 /error/506.html;
        error_page 507 /error/507.html;
        error_page 509 /error/509.html;
        error_page 510 /error/510.html;
        include D:/phpstudy_pro/WWW/GatewayWorker/GatewayWorker/nginx.htaccess;
        autoindex  off;
    }
    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9002;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }

}

wss:ip连接:
wss:域名 连接:

69 1 0
1个回答

nitron

把start_gateway里的

$gateway->transport = 'ssl';

去掉

×
🔝