From 02960a648ae3eceb92cc7bfef9b21ea0b33bf359 Mon Sep 17 00:00:00 2001 From: wonderful60 <924211739@qq.com> Date: Tue, 14 Aug 2018 18:50:49 +0800 Subject: [PATCH] Add https support --- .gitignore | 2 ++ Applications/Todpole/Web/js/Settings.js | 8 +++++- Applications/Todpole/start_gateway.php | 33 ++++++++++++++++++++++++- Applications/Todpole/start_web.php | 26 +++++++++++++++++++ README.md | 6 +++-- 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index e6936ed..6e0d551 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .settings/org.eclipse.php.core.prefs .project workerman/logs +vendor +composer.lock \ No newline at end of file diff --git a/Applications/Todpole/Web/js/Settings.js b/Applications/Todpole/Web/js/Settings.js index c3a11c7..4f74d1b 100644 --- a/Applications/Todpole/Web/js/Settings.js +++ b/Applications/Todpole/Web/js/Settings.js @@ -9,6 +9,12 @@ var Settings = function() { else { // 运行在其它域名上 - this.socketServer = 'ws://'+document.domain+':8282'; + if("https:" == document.location.protocol){ + // wss + this.socketServer = 'wss://'+document.domain+':8283'; + }else{ + // ws + this.socketServer = 'ws://'+document.domain+':8282'; + } } } diff --git a/Applications/Todpole/start_gateway.php b/Applications/Todpole/start_gateway.php index a1bfa97..2ce3185 100644 --- a/Applications/Todpole/start_gateway.php +++ b/Applications/Todpole/start_gateway.php @@ -19,10 +19,21 @@ require_once __DIR__ . '/../../vendor/autoload.php'; +// 已经申请了证书(pem/crt文件及key文件)放在了/etc/nginx/conf.d/ssl下 +// 证书最好是申请的证书 +$context = array( + 'ssl' => array( + 'local_cert' => '/etc/nginx/conf.d/ssl/server.pem', // 也可以是crt文件 + 'local_pk' => '/etc/nginx/conf.d/ssl/server.key', + 'verify_peer' => false, + ), +); + +// ws // gateway 进程 $gateway = new Gateway("Websocket://0.0.0.0:8282"); // gateway名称,status方便查看 -$gateway->name = 'TodpoleGateway'; +$gateway->name = 'TodpoleGatewayWs'; // gateway进程数 $gateway->count = 4; // 本机ip,分布式部署时使用内网ip @@ -37,6 +48,26 @@ // 服务注册地址 $gateway->registerAddress = '127.0.0.1:1237'; +// wss +// gateway 进程 +$gateway_wss = new Gateway("Websocket://0.0.0.0:8283", $context); +// 设置transport开启ssl,websocket+ssl即wss +$gateway_wss->transport = 'ssl'; +// gateway名称,status方便查看 +$gateway_wss->name = 'TodpoleGatewayWss'; +// gateway进程数 +$gateway_wss->count = 4; +// 本机ip,分布式部署时使用内网ip +$gateway_wss->lanIp = '127.0.0.1'; +// 内部通讯起始端口,假如$gateway_wss->count=4,起始端口为3700 +// 则一般会使用3701 3702 3703 3704 4个端口作为内部通讯端口 +$gateway_wss->startPort = 3700; +// 心跳间隔 +$gateway_wss->pingInterval = 10; +// 心跳数据 +$gateway_wss->pingData = '{"type":"ping"}'; +// 服务注册地址 +$gateway_wss->registerAddress = '127.0.0.1:1237'; /* // 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调 diff --git a/Applications/Todpole/start_web.php b/Applications/Todpole/start_web.php index 340eb30..4fc113b 100644 --- a/Applications/Todpole/start_web.php +++ b/Applications/Todpole/start_web.php @@ -19,13 +19,39 @@ require_once __DIR__ . '/../../vendor/autoload.php'; +// 已经申请了证书(pem/crt文件及key文件)放在了/etc/nginx/conf.d/ssl下 +// 证书最好是申请的证书 +$context = array( + 'ssl' => array( + 'local_cert' => '/etc/nginx/conf.d/ssl/server.pem', // 也可以是crt文件 + 'local_pk' => '/etc/nginx/conf.d/ssl/server.key', + 'verify_peer' => false, + ), +); + +// HTTP // WebServer $web = new WebServer("http://0.0.0.0:8383"); +// WebServer名称 +$web->name = 'TodpoleWebserverHttp'; // WebServer数量 $web->count = 2; // 设置站点根目录 $web->addRoot('www.your_domain.com', __DIR__.'/Web'); +// HTTPS +// WebServer +// https默认是用443端口,需要root运行,这里设置为8443端口,不需要root运行 +$web_https = new WebServer("https://0.0.0.0:8443", $context); +// WebServer名称 +$web_https->name = 'TodpoleWebserverHttps'; +// WebServer数量 +$web_https->count = 2; +// 设置transport开启ssl,变成http+SSL即https +$web_https->transport = 'ssl'; +// 设置站点根目录 +$web_https->addRoot('www.your_domain.com', __DIR__ . '/Web'); + // 如果不是在根目录启动,则运行runAll方法 if(!defined('GLOBAL_START')) diff --git a/README.md b/README.md index a550243..faf3ca8 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,11 @@ workerman-todpole 2、运行composer install 安装 -3、linux系统命令行进入目录运行 php start.php start -d。windows系统启动方法为双击start_for_win.bat +3、根据自己的实际情况修改Applications/Todpole/start_web.php中ssl证书的路径、域名或端口等 -4、浏览器访问地址 http://ip:8383 (ip为服务器ip)如图:(如果无法打开页面请尝试关闭服务器防火墙) +4、linux系统命令行进入目录运行 php start.php start -d。windows系统启动方法为双击start_for_win.bat + +5、浏览器访问地址 http://ip:8383 (ip为服务器ip)如图:(如果无法打开页面请尝试关闭服务器防火墙) ## 虚拟空间(静态空间、php、jsp、asp等)安装部署 虚拟空间安装请使用这个包 [网页空间版本](https://github.com/walkor/workerman-todpole-web)