fix: 通知 SSE 长连接被 Nginx 60s 超时掐断

This commit is contained in:
2026-08-05 18:00:45 +08:00
parent 2a519a3714
commit 32343b271b
3 changed files with 35 additions and 13 deletions

View File

@@ -25,6 +25,22 @@ server {
add_header Cache-Control "no-cache"; add_header Cache-Control "no-cache";
} }
# SSE 长连接:禁用代理缓冲并放宽读写超时,避免 60s 空闲被掐断
location ~ ^/api/(notifications/stream|attendance-records/import/dingtalk/stream|ai/chat/.*/stream)$ {
proxy_pass http://backend:3000;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
add_header X-Accel-Buffering no;
}
location /api/ { location /api/ {
proxy_pass http://backend:3000/api/; proxy_pass http://backend:3000/api/;
proxy_set_header Host $host; proxy_set_header Host $host;

View File

@@ -58,6 +58,7 @@ const NotificationBell: React.FC = () => {
const token = useUserStore.getState().token; const token = useUserStore.getState().token;
if (!token) return; if (!token) return;
const es = new EventSource(`/api/notifications/stream?token=${encodeURIComponent(token)}`); const es = new EventSource(`/api/notifications/stream?token=${encodeURIComponent(token)}`);
es.onopen = () => setSseDown(false);
es.onmessage = (event) => { es.onmessage = (event) => {
try { try {
JSON.parse(event.data); JSON.parse(event.data);
@@ -68,7 +69,7 @@ const NotificationBell: React.FC = () => {
} }
}; };
es.onerror = () => { es.onerror = () => {
es.close(); // 不主动关闭EventSource 会自动重连,主动关闭会导致一次超时后实时通知永久断流
setSseDown(true); setSseDown(true);
}; };
return () => { return () => {

View File

@@ -9,7 +9,7 @@ import {
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { Request } from 'express'; import { Request } from 'express';
import { Observable, map } from 'rxjs'; import { Observable, interval, map, merge } from 'rxjs';
import { NotificationsService } from './notifications.service'; import { NotificationsService } from './notifications.service';
import { NotificationQueryDto } from './dto/notification.dto'; import { NotificationQueryDto } from './dto/notification.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
@@ -53,17 +53,22 @@ export class NotificationsController {
req.on('close', () => { req.on('close', () => {
this.service.unsubscribe(userId); this.service.unsubscribe(userId);
}); });
return this.service.subscribe(userId).pipe( // 每 25s 发送一次空消息作为心跳,避免空闲连接被 Nginx 等中间层超时掐断。
map((notification) => ({ // 空 data 会被前端 EventSource 收到并忽略JSON.parse 失败)。
data: JSON.stringify({ return merge(
id: notification.id, this.service.subscribe(userId).pipe(
type: notification.type, map((notification) => ({
title: notification.title, data: JSON.stringify({
content: notification.content, id: notification.id,
link: notification.link, type: notification.type,
createdAt: notification.createdAt, title: notification.title,
}), content: notification.content,
} as MessageEvent)), link: notification.link,
createdAt: notification.createdAt,
}),
} as MessageEvent)),
),
interval(25_000).pipe(map(() => ({ data: '' } as MessageEvent))),
); );
} }