环境:windows 2012 r2 + php 7.4.2
不开启SSL的时候,一切正常,连续开机测试过2天都没问题
但是自从配置了SSL之后,每隔一个小时以后就会出现服务端提示推送成功,但客户端无法收到数据的问题。
此时,客户端重启打开浏览器,可以正常连接并发送心跳!服务端进行推送,也会有数据返回。麻烦各位帮忙分析一下!非要用windows环境,我也确实搞不懂哪里出问题,才来这里进行提问!
代码如下
<?php
require 'vendor/autoload.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Connection\TcpConnection;
use GlobalData\Client as GlobalClient;
use Channel\Client as ChannelClient;
//证书
$context = array(
'ssl' => array(
'local_cert' => 'D:\wss\ssl\ws.pem',
'local_pk' => 'D:\wss\ssl\ws.key',
'verify_peer' => false,
)
);
$WsWorker = new Worker('websocket://0.0.0.0:443', $context);
$WsWorker->transport = 'ssl';
$WsWorker->count = 4;
$WsWorker->name = 'WebSocket';
$WsWorker->onWorkerStart = function ($worker){
$config = require dirname(__FILE__).'/config.php';
$worker->apiKey = strtolower($config['api_key']);
$worker->topics = array_map('strtolower',$config['topics']);
$worker->global = new GlobalClient('127.0.0.1:2207');
$worker->channel = new ChannelClient();
$worker->channel->connect('127.0.0.1:2206');
Timer::add(45,function()use($worker){
$cur_time = time();
foreach($worker->connections as $conn){
if(!isset($conn->lastAccessTime)){
$conn->lastAccessTime = time();
continue;
}
if($cur_time-$conn->lastAccessTime>=50){
echo "$cur_time<>{$conn->lastAccessTime}\r\n";
$conn->close();
}
}
});
$worker->channel->on('publish',function($data)use($worker){
$data_json = @json_decode($data,true);
if(error_get_last()){
return;
}
$topic = strtolower($data_json['topic']);
$title = strtolower($data_json['title']);
$content = strtolower($data_json['content']);
$link = strtolower($data_json['link']);
$stock = strtolower($data_json['stock']);
$send_data = [
'ctrl'=>'send',
'params'=>[
'title'=>$title,
'content'=>$content,
'link'=>$link,
'stock'=>$stock,
'topic'=>$topic,
],
'msg'=>'',
];
foreach($worker->connections as $conn){
if(isset($conn->topic) && strtolower($topic) == strtolower($conn->topic)){
$conn->send(json_encode($send_data));
}
}
});
};
$WsWorker->onMessage = function ($connection,$data){
$connection->lastAccessTime = time();
$worker = $connection->worker;
$data_json = @json_decode($data,true);
if(json_last_error()){
$connection->close();
return;
}
$ctrl = strtolower($data_json['ctrl'] ?? null);
$params = !isset($data_json['params']) || !is_array($data_json['params']) ? array() : $data_json['params'];
if(!$ctrl){
$connection->close();
return;
}elseif($ctrl == 'bind'){
$token = $params['token'] ?? '';
if($token && isset($worker->global->$token) && $worker->global->$token){
$connection->topic = $worker->global->$token;
$connection->send('{"ctrl":"ok","data":{},"msg":""}');
unset($worker->global->$token);
}else{
$connection->send('{"ctrl":"fail","data":{},"msg":"error token"}');
}
}
};
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
?>
尝试用nginx代理下ssl