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