为啥我用workerman发送给客户端数据的header里面有两个 Content-Type ?

小七他哥

问题描述

问题如图:

我只截取了api接口的信息,其实所有http请求都有两个 Content-Type,包括 html js css 图片文件等。

程序代码或配置

<?php

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Protocols\Websocket;
use Workerman\Timer;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/../config.php';
include_once __DIR__ . '/lib/MySQL.php';
include_once __DIR__ . '/lib/function.php';
require_once __DIR__ . '/api.php';
require_once __DIR__ . '/ws.php';

Worker::$pidFile = __DIR__ . '/runtime/workerman.pid';
Worker::$logFile = __DIR__ . '/runtime/workerman-' . date('m') . '.log';

$worker = new Worker('http://0.0.0.0:8053');

// 启动 2 个进程对外提供服务
$worker->count = 2;
$worker->name = 'myServer';

$worker->onMessage = function (TcpConnection $connection, $request) {
    if (!($request instanceof Request)) {
        //print_r($request) . "\r\n";
        return false;
    }

    if ($request->header('upgrade') === 'websocket') {
        $connection->protocol = Websocket::class;
        Websocket::input($request, $connection);
        if ($sid = $request->get('sid')) {
            $connection->sid = $sid;
            $connection->num = (isset(WsService::$connectionList[$sid]) ? count(WsService::$connectionList[$sid]) : 0) + 1;
            WsService::$connectionList[$sid][$connection->num] = $connection;
        }
        return false;
    }

    // 获取请求的路径
    $path = $request->path();

    // 如果请求根路径 '/', 则默认返回 index.html
    if ($path == '/') {
        $path = '/index.html';
    }

    // 获取文件的绝对路径
    $file_path = __DIR__ . '/../' . $path;

    if (!file_exists($file_path)) {
        echo $file_path . "不存在\r\n";
        return $connection->send(new Response(404, ['Content-Type' => 'text/html'], '<h1>404 Not Found</h1>'));
    }

    $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
    if ($ext === 'php') {
        $header = [
            'Access-Control-Allow-Origin' => '*',
            'content-type' => 'application/json'
        ];

        try {
            $api = new Api($request);
        } catch (Exception $exception) {
            return $connection->send(new Response(200, $header, json_encode(['errcode' => $exception->getCode(), 'msg' => $exception->getMessage()])));
        }

        $connection->send(new Response(200, $api->header ? array_replace($header, $api->header) : $header, $api->result));
    } else {
        if (!is_dir($file_path)) {
            $connection->send(new Response(200, getFileHeader($ext), file_get_contents($file_path)));
        }
    }
};

$worker->onClose = function (TcpConnection $connection) {
    if (isset($connection->sid) && WsService::$connectionList) {
        foreach (WsService::$connectionList as $sid => $item) {
            if ($sid === $connection->sid && isset($item[$connection->num])) {
                unset(WsService::$connectionList[$sid][$connection->num]);
            }
        }
    }
};

// 运行所有 worker
Worker::runAll();

操作系统环境及workerman/webman等具体版本

Windows 10 家庭中文版

composer.json 文件内容如下:

207 1 0
1个回答

walkor 打赏
$header = [
            'Access-Control-Allow-Origin' => '*',
            'content-type' => 'application/json'
        ];

改成

$header = [
            'Access-Control-Allow-Origin' => '*',
            'Content-Type' => 'application/json'
        ];
×
🔝