57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ln.http.websocket;
|
|
using ln.logging;
|
|
using System.IO;
|
|
using ln.types;
|
|
using System.Text;
|
|
using System.Linq;
|
|
|
|
namespace ln.http.resources.websocket
|
|
{
|
|
public class WebsocketResource : Resource
|
|
{
|
|
public Func<HttpRequest,WebSocket> WebSocketFactory { get; }
|
|
|
|
public IEnumerable<WebSocket> CurrentWebSockets { get { lock (this) { return currentWebSockets.ToArray(); }; } }
|
|
List<WebSocket> currentWebSockets = new List<WebSocket>();
|
|
|
|
public WebsocketResource(Resource container,String resourceName, Func<HttpRequest, WebSocket> webSocketFactory)
|
|
:base(container,resourceName)
|
|
{
|
|
WebSocketFactory = webSocketFactory;
|
|
}
|
|
|
|
public override void AddResource(Resource resource) => throw new NotImplementedException();
|
|
public override bool Contains(string name) => throw new NotImplementedException();
|
|
public override IEnumerable<Resource> GetResources() => throw new NotImplementedException();
|
|
public override void RemoveResource(Resource resource) => throw new NotImplementedException();
|
|
|
|
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
|
{
|
|
WebSocket webSocket = null;
|
|
try
|
|
{
|
|
webSocket = WebSocketFactory(httpRequest);
|
|
lock (this)
|
|
currentWebSockets.Add(webSocket);
|
|
webSocket.Run();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logging.Log(e);
|
|
}
|
|
finally
|
|
{
|
|
if (webSocket != null)
|
|
lock (this)
|
|
currentWebSockets.Remove(webSocket);
|
|
}
|
|
|
|
httpRequest.GetConnectionStream().Close();
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|