请教,我用webman的自定义进程来实现event-stream,前端是umi,遇到一个代理的问题

yysu

问题描述

我用webman的自定义进程来实现event-stream,前端是umi,遇到一个代理的问题。
前端端口为8000,webman的自定义进程是8686,因此,在umi,中做了proxy代理,代理到8686端口来访问进程。出现的现象是,前端访问代理接口报错
[HPM] Error occurred while proxying request localhost:8000/stream to http://127.0.0.1:8686/ [HPE_INVALID_HEADER_TOKEN]
我谷歌了这个错误,"由服务器响应错误/格式错误引起",但是直接访问 http://127.0.0.1:8686/ 是可以正确输出的,进程中的代码使用的网站中的示例代码

if ($request->header('accept') === 'text/event-stream') {
            // 首先发送一个 Content-Type: text/event-stream 头的响应
            $connection->send(new Response(200, [
                'Content-Type' => 'text/event-stream',
                'Cache-Control'=> 'no-cache',
                'Connection'=> 'keep-alive'
            ]));
            // 定时向客户端推送数据
            $timer_id = Timer::add(2, function () use ($connection, &$timer_id){
                // 连接关闭的时候要将定时器删除,避免定时器不断累积导致内存泄漏
                if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
                    Timer::del($timer_id);
                    return;
                }
                // 发送message事件,事件携带的数据为hello,消息id可以不传
                $connection->send(new ServerSentEvents(['event' => 'message', 'data' => 'hello', 'id'=>1]));
            });
            return;
        }
        // $connection->send('ok');

代理部分如下

proxy: {
    '/api': {
      target: 'http://127.0.0.1:8787',
      // pathRewrite: { '^/api': '' },
      changeOrigin: true,
    },
    '/stream': {
      target: 'http://127.0.0.1:8686',
      // pathRewrite: { '^/api': '' },
      changeOrigin: true,
    }
  }

如何解决这个问题呢

578 0 0
0个回答

年代过于久远,无法发表回答
🔝