43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
|
using Tesses.WebServer;
|
|||
|
using Tesses.WebServer.WebSocket;
|
|||
|
|
|||
|
List<Func<WebSocketMessage,Task>> sendTo=new List<Func<WebSocketMessage, Task>>();
|
|||
|
|
|||
|
MountableServer svr=new MountableServer(new StaticServer("www"));
|
|||
|
RouteServer rsvr=new RouteServer();
|
|||
|
svr.Mount("/api/",rsvr);
|
|||
|
rsvr.Add("/socket",async(ctx)=>{
|
|||
|
Func<WebSocketMessage,Task>? f=null;
|
|||
|
await ctx.StartWebSocketConnectionAsync(async(sendMessage,ping,token)=>{
|
|||
|
|
|||
|
f=sendMessage;
|
|||
|
await sendMessage(WebSocketMessage.Create("Hello"));
|
|||
|
lock(sendTo){
|
|||
|
sendTo.Add(sendMessage);
|
|||
|
}
|
|||
|
while(!token.IsCancellationRequested)
|
|||
|
{
|
|||
|
await Task.Delay(100);
|
|||
|
}
|
|||
|
},async(message)=>{
|
|||
|
List<Func<WebSocketMessage,Task>> sendTo2=new List<Func<WebSocketMessage,Task>>();
|
|||
|
lock(sendTo)
|
|||
|
{
|
|||
|
sendTo2.AddRange(sendTo);
|
|||
|
}
|
|||
|
|
|||
|
Console.WriteLine($"Message: {message.Text}");
|
|||
|
|
|||
|
foreach(var item in sendTo2)
|
|||
|
{
|
|||
|
await item(message);
|
|||
|
}
|
|||
|
|
|||
|
},(clean)=>{
|
|||
|
lock(sendTo){
|
|||
|
if(f!= null)
|
|||
|
sendTo.Remove(f);
|
|||
|
}
|
|||
|
});
|
|||
|
});
|
|||
|
svr.StartServer(4449);
|