Implementation:A616567126 GPT WEB JAVA SseController
| Knowledge Sources | |
|---|---|
| Domains | SSE, Real_Time_Communication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete REST controller for establishing and closing Server-Sent Events (SSE) connections for real-time message delivery to authenticated users.
Description
The SseController exposes two GET endpoints under `/sse`: `/connect/{userId}` creates an SSE connection after validating the user's JWT token from Redis, and `/close/{userId}` removes the SSE connection. It delegates connection management to `SseEmitterServer` and uses `RedisUtil` for token validation.
Usage
Use this controller when a client needs to establish a persistent SSE connection for receiving real-time AI chat responses, or when closing an existing connection.
Code Reference
Source Location
- Repository: A616567126_GPT_WEB_JAVA
- File: src/main/java/com/intelligent/bot/api/sse/SseController.java
- Lines: 1-42
Signature
@RestController
@RequestMapping("/sse")
public class SseController {
@GetMapping("/connect/{userId}")
public SseEmitter connect(@PathVariable Long userId);
@GetMapping("/close/{userId}")
public B<String> close(@PathVariable("userId") Long userId);
}
Import
import com.intelligent.bot.api.sse.SseController;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| userId | Long | Yes | User ID from path variable |
Outputs
| Name | Type | Description |
|---|---|---|
| connect returns | SseEmitter | SSE emitter for streaming (null if not authenticated) |
| close returns | B<String> | Success response with "连接关闭" |
Usage Examples
Establishing SSE Connection
// GET /sse/connect/12345
// Authorization: Bearer <jwt-token>
// Response: text/event-stream
// Server returns SseEmitter for streaming messages
Closing SSE Connection
// GET /sse/close/12345
// Response:
// { "status": 200, "message": "操作成功", "data": "连接关闭" }