1+ using System ;
2+ using System . Threading ;
3+ using System . Threading . Tasks ;
4+ using GameFrameX . SuperSocket . Http ;
5+ using GameFrameX . SuperSocket . Server . Abstractions . Session ;
6+ using SuperSocket . Connection ;
7+
8+ namespace SuperSocket . Http
9+ {
10+ /// <summary>
11+ /// Extension methods for easier HTTP functionality usage.
12+ /// </summary>
13+ public static class HttpExtensions
14+ {
15+ /// <summary>
16+ /// Sends an HTTP response to the session.
17+ /// </summary>
18+ /// <param name="session">The session to send the response to.</param>
19+ /// <param name="response">The HTTP response to send.</param>
20+ /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
21+ /// <returns>A task that represents the asynchronous operation.</returns>
22+ public static async ValueTask SendHttpResponseAsync (
23+ this IAppSession session ,
24+ HttpResponse response ,
25+ CancellationToken cancellationToken = default )
26+ {
27+ await session . SendAsync ( HttpResponseEncoder . Instance , response , cancellationToken ) ;
28+ }
29+
30+ /// <summary>
31+ /// Sends a simple HTTP response with the specified status and body.
32+ /// </summary>
33+ /// <param name="session">The session to send the response to.</param>
34+ /// <param name="statusCode">The HTTP status code.</param>
35+ /// <param name="body">The response body.</param>
36+ /// <param name="contentType">The content type. Defaults to "text/plain".</param>
37+ /// <param name="keepAlive">Whether to keep the connection alive.</param>
38+ /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
39+ /// <returns>A task that represents the asynchronous operation.</returns>
40+ public static async ValueTask SendHttpResponseAsync (
41+ this IAppSession session ,
42+ int statusCode ,
43+ string body = "" ,
44+ string contentType = "text/plain" ,
45+ bool keepAlive = true ,
46+ CancellationToken cancellationToken = default )
47+ {
48+ var response = new HttpResponse ( statusCode ) ;
49+ response . SetContentType ( contentType ) ;
50+ response . Body = body ;
51+ response . KeepAlive = keepAlive ;
52+
53+ await session . SendHttpResponseAsync ( response , cancellationToken ) ;
54+ }
55+
56+ /// <summary>
57+ /// Sends a JSON HTTP response.
58+ /// </summary>
59+ /// <param name="session">The session to send the response to.</param>
60+ /// <param name="jsonData">The JSON data to send.</param>
61+ /// <param name="statusCode">The HTTP status code. Defaults to 200.</param>
62+ /// <param name="keepAlive">Whether to keep the connection alive.</param>
63+ /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
64+ /// <returns>A task that represents the asynchronous operation.</returns>
65+ public static async ValueTask SendJsonResponseAsync (
66+ this IAppSession session ,
67+ string jsonData ,
68+ int statusCode = 200 ,
69+ bool keepAlive = true ,
70+ CancellationToken cancellationToken = default )
71+ {
72+ await session . SendHttpResponseAsync ( statusCode , jsonData , "application/json" , keepAlive , cancellationToken ) ;
73+ }
74+
75+ /// <summary>
76+ /// Creates a Server-Sent Events writer for the session.
77+ /// </summary>
78+ /// <param name="session">The session to create the SSE writer for.</param>
79+ /// <param name="options">Optional SSE configuration options.</param>
80+ /// <returns>A new ServerSentEventWriter instance.</returns>
81+ public static ServerSentEventWriter CreateSSEWriter ( this IAppSession session , ServerSentEventsOptions options = null )
82+ {
83+ return new ServerSentEventWriter ( session . Connection , options ) ;
84+ }
85+
86+ /// <summary>
87+ /// Starts a Server-Sent Events stream for the session.
88+ /// </summary>
89+ /// <param name="session">The session to start SSE for.</param>
90+ /// <param name="options">Optional SSE configuration options.</param>
91+ /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
92+ /// <returns>A new ServerSentEventWriter instance that has already sent the initial response.</returns>
93+ public static async ValueTask < ServerSentEventWriter > StartSSEAsync (
94+ this IAppSession session ,
95+ ServerSentEventsOptions options = null ,
96+ CancellationToken cancellationToken = default )
97+ {
98+ var writer = session . CreateSSEWriter ( options ) ;
99+ await writer . SendInitialResponseAsync ( cancellationToken ) ;
100+ return writer ;
101+ }
102+
103+ /// <summary>
104+ /// Checks if the HTTP request accepts Server-Sent Events.
105+ /// </summary>
106+ /// <param name="request">The HTTP request to check.</param>
107+ /// <returns>True if the request accepts SSE, false otherwise.</returns>
108+ public static bool IsSSERequest ( this HttpRequest request )
109+ {
110+ return request . AcceptsEventStream ;
111+ }
112+
113+ /// <summary>
114+ /// Checks if the HTTP request wants to keep the connection alive.
115+ /// </summary>
116+ /// <param name="request">The HTTP request to check.</param>
117+ /// <returns>True if keep-alive is requested, false otherwise.</returns>
118+ public static bool IsKeepAliveRequest ( this HttpRequest request )
119+ {
120+ return request . KeepAlive ;
121+ }
122+ }
123+ }
0 commit comments