可以考虑一下,真机连接的后端的相关sse接口有没有禁用nginx缓存,需要手动禁用缓存。可以参考: # 设置 Nginx 不对 SSE 响应进行缓冲,直接透传给客户端 proxy_buffering off; # 设置代理读取服务器响应的超时时间 proxy_read_timeout 24h; # 设置客户端连接的超时时间 proxy_connect_timeout 1h; # 设置 HTTP 版本,SSE 需要 HTTP/1.1 proxy_http_version 1.1; # 保持连接活性,不发送连接关闭的信号 proxy_set_header Connection ''; # 配置代理传递的头部,确保 Host 头部正确传递 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 设置代理的响应头部,保持传输编码为 chunked proxy_set_header X-Accel-Buffering no; # 设置流式响应头 proxy_set_header Accept "text/event-stream"; proxy_set_header Cache-Control "no-cache"; # 禁用压缩 proxy_set_header Accept-Encoding ""; # 设置跨域资源共享 (CORS) add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With,Content-Type' always;
RequestTask.onChunkReceived数据丢失(本地丢失少,线上丢失过多)?springboot后端SSE流式接口看日志是返回完整的markdow格式,但是RequestTask.onChunkReceived出现数据部分丢失(本地的微信开发工具调试少有丢失,线上版本丢失很严重,用的都是正式版本的后端接口),导致markdown格式化成文本出错,前端内容出现错位及阅读性障碍。麻烦告知解决方案,本地与线上数据丢失偏移过大。
08-27可以考虑一下,真机连接的后端的相关sse接口有没有禁用nginx缓存,需要手动禁用缓存。可以参考: # 设置 Nginx 不对 SSE 响应进行缓冲,直接透传给客户端 proxy_buffering off; # 设置代理读取服务器响应的超时时间 proxy_read_timeout 24h; # 设置客户端连接的超时时间 proxy_connect_timeout 1h; # 设置 HTTP 版本,SSE 需要 HTTP/1.1 proxy_http_version 1.1; # 保持连接活性,不发送连接关闭的信号 proxy_set_header Connection ''; # 配置代理传递的头部,确保 Host 头部正确传递 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 设置代理的响应头部,保持传输编码为 chunked proxy_set_header X-Accel-Buffering no; # 设置流式响应头 proxy_set_header Accept "text/event-stream"; proxy_set_header Cache-Control "no-cache"; # 禁用压缩 proxy_set_header Accept-Encoding ""; # 设置跨域资源共享 (CORS) add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With,Content-Type' always;
RequestTask.onChunkReceived 接受部分数据丢失 后端日志返回了完整数据?确认过 后端返回的数据是完整的 但前端接受的数据 经常 会部分丢失 或 格式错乱 有解决的方法吗 急等 [图片][图片]
08-27可以考虑一下,真机连接的后端的相关sse接口有没有禁用nginx缓存,需要手动禁用缓存。可以参考: # 设置 Nginx 不对 SSE 响应进行缓冲,直接透传给客户端 proxy_buffering off; # 设置代理读取服务器响应的超时时间 proxy_read_timeout 24h; # 设置客户端连接的超时时间 proxy_connect_timeout 1h; # 设置 HTTP 版本,SSE 需要 HTTP/1.1 proxy_http_version 1.1; # 保持连接活性,不发送连接关闭的信号 proxy_set_header Connection ''; # 配置代理传递的头部,确保 Host 头部正确传递 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 设置代理的响应头部,保持传输编码为 chunked proxy_set_header X-Accel-Buffering no; # 设置流式响应头 proxy_set_header Accept "text/event-stream"; proxy_set_header Cache-Control "no-cache"; # 禁用压缩 proxy_set_header Accept-Encoding ""; # 设置跨域资源共享 (CORS) add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With,Content-Type' always;
RequestTask.onChunkReceived本地环境一个字一个字展示,真机就是一整段展示?[图片]
08-27可以考虑一下,真机连接的后端的相关sse接口有没有禁用nginx缓存,需要手动禁用缓存。可以参考: # 设置 Nginx 不对 SSE 响应进行缓冲,直接透传给客户端 proxy_buffering off; # 设置代理读取服务器响应的超时时间 proxy_read_timeout 24h; # 设置客户端连接的超时时间 proxy_connect_timeout 1h; # 设置 HTTP 版本,SSE 需要 HTTP/1.1 proxy_http_version 1.1; # 保持连接活性,不发送连接关闭的信号 proxy_set_header Connection ''; # 配置代理传递的头部,确保 Host 头部正确传递 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 设置代理的响应头部,保持传输编码为 chunked proxy_set_header X-Accel-Buffering no; # 设置流式响应头 proxy_set_header Accept "text/event-stream"; proxy_set_header Cache-Control "no-cache"; # 禁用压缩 proxy_set_header Accept-Encoding ""; # 设置跨域资源共享 (CORS) add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With,Content-Type' always;
requestTask.onChunkReceived为什么真机和开发者工具触发的次数不一样?[图片]这个时真机返回的数据,并且数据被切割了 [图片]这个时模拟器触发的,每次都是完整的 // 2. 发起分块流式请求 const requestTask = wx.request({ url: `https://lawapi.chkjnotary.com/api/MiniProgram/Chat/Contract/Inspect/SimpleTest`, method: "POST", header: { Accept: "text/event-stream", "Auth-token": '65d0e62d283542bfb4eb74ca705201847', 'Content-Type': 'text/plain; charset=utf-8;' }, data, dataType: "json", responseType: "text", enableChunked: true, success(res) { console.log("连接成功") }, fail(err) { console.error("请求失败:", err) }, }) // 3. 监听数据流(核心逻辑) requestTask.onChunkReceived(async (res) => { console.log('res', res.data); })
08-27