@@ -70,12 +70,16 @@ static async Task Rewrite() {
7070
7171 #endregion https
7272
73+ server . MapGet ( "/api/syslog/index" , object ( string ? query = null ) => {
74+ return new { query } ;
75+ } ) ;
76+
7377 server . MapGet ( "/api/test/hello" , object ( HttpSession session ) => {
7478 return new { message = $ "Hello World !" } ;
7579 } ) ;
7680
7781 server . UseStaticFilesModule ( options => {
78- options . Path = @"C:\www\spa\websocket \" ;
82+ options . Path = @"C:\www\spa\sse \" ;
7983 options . Prefix = "/" ;
8084 options . AutoIndex = true ;
8185 options . CacheTimeout = TimeSpan . FromDays ( 1 ) ;
@@ -139,6 +143,17 @@ static string ChatEvent(string kind, string room, string name, string? text = nu
139143 await conn . SendTextAsync ( msg . IsJson ? $ "unknown op: { msg . Op } " : "bad message: expected JSON {op,payload}" ) ;
140144 } ) ;
141145
146+ ws . OnConnect = async ( conn , ctx ) => {
147+ IWebUser ? user = ctx . Session . Request . User ;
148+ Console . WriteLine ( $ "websocket connect { user ? . Id } { user ? . Login } ") ;
149+ if ( user == null ) {
150+ return ;
151+ }
152+ //await ctx.JoinRoomAsync(user.Id.ToString(), conn);
153+ //if (user.IsInRoles("admin, task:admin")) {
154+ // await ctx.JoinRoomAsync("task", conn);
155+ //}
156+ } ;
142157 ws . OnDisconnect = async ( conn , ctx ) => {
143158 // cleanup
144159 if ( roomByConn . TryRemove ( conn . Id , out var room ) && userByConn . TryRemove ( conn . Id , out var name ) ) {
@@ -147,14 +162,47 @@ static string ChatEvent(string kind, string room, string name, string? text = nu
147162 } ;
148163 } ) ;
149164
165+ ServerSentEventsHub ? hub = null ;
166+
167+ server . UseServerSentEventsModule ( opt => {
168+ opt . Prefix = "/sse" ;
169+ opt . AllowAnyOrigin = true ; // pratique si tu ouvres index.html en file:// ou autre origin
170+ opt . AutoJoinRoom = "__all" ; // tout le monde dans la room globale
171+ opt . KeepAliveInterval = TimeSpan . FromSeconds ( 15 ) ;
172+
173+ hub = opt . Hub ;
174+
175+ opt . OnConnect = async ( conn , ctx ) => {
176+ // Message de bienvenue (optionnel)
177+ await conn . SendEventAsync ( "connected" , @event : "status" ) ;
178+ } ;
179+ } ) ;
180+
181+ // Timer: broadcast toutes les 5 secondes
182+ _ = Task . Run ( async ( ) => {
183+ int i = 0 ;
184+ while ( true ) {
185+ await Task . Delay ( TimeSpan . FromSeconds ( 5 ) ) ;
186+
187+ if ( hub is null )
188+ continue ;
189+
190+ i ++ ;
191+ await hub . BroadcastAsync ( "__all" , new ServerSentEventsMessage {
192+ Event = "tick" ,
193+ Payload = $ "tick #{ i } @ { DateTime . UtcNow : O} "
194+ } ) ;
195+ }
196+ } ) ;
197+
150198 //server.MapControllers<SubController>("/api");
151199 //server.MapControllers<Controller>("/api");
152200
153201 server . Configure ( options => {
154202 options . ReuseAddress = true ;
155203 options . TcpNoDelay = true ;
156204 options . TcpKeepAlive = true ;
157- options . JwtOptions = new JwtOptions ( "minimumlongsecret " ) {
205+ options . JwtOptions = new JwtOptions ( "azertyuiopqsdfghjklmwxcvbn " ) {
158206 ValidateExp = false ,
159207 ValidateNbf = false ,
160208 } ;
0 commit comments