链接wss,在服务器上运行报错,提示Sec-WebSocket-Accept not found

liuhua

问题描述

用AsyncTcpConnection链接wss,在自己的电脑上都能行,在服务器上就不行了,提示Sec-WebSocket-Accept not found

程序代码

<?php

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker();
$worker->count = 1;
// 进程启动时
$worker->onWorkerStart = function ($onWorker) {
    $ws_connection = new AsyncTcpConnection("ws://stream.binance.com:443/stream?streams=luncusdt@ticker");
    $ws_connection->transport = 'ssl';
    // 当TCP完成三次握手后
    $ws_connection->onConnect = function ($connection) {
        echo "连接成功,开始订阅币安现货信息\n";

    };
    // 远程websocket服务器发来消息时
    $ws_connection->onMessage = function ($connection, $data) {
        var_dump($data);
        $data = json_decode($data, true);

        //心跳检测
        if (isset($data['ping'])) {
            $msg = ["pong" => $data['ping']];
            $connection->send(json_encode($msg));
        }
    };
    // 连接上发生错误时,一般是连接远程websocket服务器失败错误
    $ws_connection->onError = function ($connection, $code, $msg) {
        echo "error: $msg\n";
    };
    // 当连接远程websocket服务器的连接断开时
    $ws_connection->onClose = function ($connection) {
        echo "connection closed and try to reconnect\n";
        // 如果连接断开,1秒后重连
        $connection->reConnect(1);
    };
    // 设置好以上各种回调后,执行连接操作
    $ws_connection->connect();
};
// 如果不是在根目录启动,则运行runAll方法
if (!defined('GLOBAL_START')) {
    Worker::runAll();
}

报错信息

Sec-WebSocket-Accept not found. Header:
HTTP/1.1 451 
Server: awselb/2.0
Date: Thu, 20 Jun 2024 04:52:17 GMT
Content-Length: 224
Connection: keep-alive
Content-Type: application/json

截图报错信息里报错文件相关代码

本地成功执行
截图
服务器上报错
截图

591 1 0
1个回答

walkor 打赏

服务器区域的?
451状态码 可能是对方websocket接口不支持是你服务器所在区域

  • miles 2024-10-22

    @liuhua 你后来咋解决的?分享下

×
🔝