Skip to content

swoole 5.1.3 谷歌应用市场机审机器客户端发起ws长连接 不兼容 被识别成http请求导致错误 #5782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
LIngMax opened this issue May 24, 2025 · 1 comment

Comments

@LIngMax
Copy link

LIngMax commented May 24, 2025

谷歌应用市场机器审核 安卓客户端发起ws长连接 不兼容 被识别成http请求
后来排查 与其他客户端区别是 少传了个 Upgrade: websocket

这种特殊问题下 至于swoole调不调整 这看你们权衡了

这边通过nginx 遇到 sec_websocket_key 增加 Upgrade: websocket 解决

map $http_sec_websocket_key $req_upgrade {
    default $http_upgrade;
    ~.+     "websocket";
}
server {
        location  /  {
            .....
            proxy_set_header Connection "upgrade";
           proxy_set_header Upgrade $req_upgrade;# map 判断 Sec-WebSocket-Key 是否存在 修复少部分机型兼容问题
       }
}

#############################

swoole 5.1.3

打印请求信息
var_dump($request->header)
{
"host": "ws.aaa.net",
"x-real_ip": "18.68....",
"x-forwarded-for": "66.249....",
"connection": "upgrade",
"accept-encoding": "gzip",
"sec-websocket-extensions": "permessage-deflate",
"sec-websocket-key": "cH8Hwg28....WElHL3Lw==",
"permessage-deflate": "true",
"user-agent": "okhttp/4.12.0",
"via": "1.1 d6022fd.....cc.cloudfront.net (CloudFront)",
"x-amz-cf-id": "5VrnwteDJZ-S.Die...Ydw==",
"sec-websocket-version": "13"
}

$server = new \Swoole\WebSocket\Server('0.0.0.0', 80,SWOOLE_PROCESS);
$server->set([
    'daemonize'             =>true,#守护进程
    'log_file'              => WORK_LOG,
    'reactor_num'           => swoole_cpu_num(),#reactor线程数 1-4倍 默认值:CPU 核数
    'worker_num'            => swoole_cpu_num(),#CPU核数的1-4倍 默认值:CPU 核数 迎接Swoole 6多线程版本
    'task_worker_num'       =>0 ,#WORK_NUM,#task进程数量


    'open_tcp_keepalive'    => true,
    'tcp_keepidle'          => 12, //n秒没有数据传输就进行检测
    'tcp_keepinterval'      => 10, //n秒探测一次
    'tcp_keepcount'         => 2, //探测的次数,超过n次后还没回包close此连接
    // 'open_tcp_nodelay'      => false,#true关闭Nagle算法 false开启Nagle算法
    
    
    'hook_flags'            => SWOOLE_HOOK_ALL,#开启协程钩子
    'enable_coroutine'      => true,#启用协程
    'task_enable_coroutine' => true,#Task启用协程
    'reload_async'          => true,#触发重启 当前无任何协程时进程才会退出
    'max_wait_time'         => 3,#重启等待超时 强杀进程
    
    
    'open_websocket_ping_frame' => true,#ws开启ping帧 
    'open_websocket_pong_frame' => true,#ws开启pong帧 
    'websocket_compression' => true,#ws压缩
    
    'http_parse_cookie'     => false,#自动解析COOKIE请求
    'http_parse_post'       => true,#自动解析POST请求
    'http_parse_files'      => false,#自动解析上传文件
    'http_compression'      => true,#http压缩 
    'http_compression_level'=> 6,#压缩级别 1-9
    'open_http_protocol'    => true,#HTTP协议解析
    
]);

#ws数据接收事件回调函数
$server->on('message',function ($server, $frame){
    $_fd = $frame->fd;
    var_dump(func_get_args());
});
#http请求事件回调函数
$server->on('request', function ($request, $response){
    global $server;
    var_dump(func_get_args());
    @$response->end("返回结果".$ret);
});
$server->start();
@NathanFreeman
Copy link
Member

我看看websocket的协议先

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants