2019-08-03 12:57:32 +02:00
|
|
|
|
using System;
|
|
|
|
|
using ln.http.resources.websocket;
|
|
|
|
|
using ln.http.resources;
|
|
|
|
|
using ln.logging;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using ln.http.websocket;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using ln.types.rpc;
|
2019-08-19 14:15:10 +02:00
|
|
|
|
using ln.json;
|
|
|
|
|
using ln.json.mapping;
|
2019-08-03 12:57:32 +02:00
|
|
|
|
|
|
|
|
|
namespace ln.skyscanner.http
|
|
|
|
|
{
|
|
|
|
|
public class WebSocketInterface : WebsocketResource
|
|
|
|
|
{
|
|
|
|
|
SkyScanner.Service SkyScannerService { get; }
|
|
|
|
|
|
|
|
|
|
public WebSocket[] CurrentWebSockets {
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (currentWebSockets)
|
|
|
|
|
{
|
|
|
|
|
return currentWebSockets.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HashSet<WebSocket> currentWebSockets = new HashSet<WebSocket>();
|
|
|
|
|
|
|
|
|
|
public WebSocketInterface(Resource container,SkyScanner.Service skyscannerService)
|
|
|
|
|
:base(container,"socket")
|
|
|
|
|
{
|
|
|
|
|
SkyScannerService = skyscannerService;
|
|
|
|
|
Connection += WebSocketInterface_Connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebSocketInterface_Connection(WebsocketResource webSocketResource, ln.http.websocket.WebSocket webSocket, WSREvent ev)
|
|
|
|
|
{
|
|
|
|
|
lock (currentWebSockets)
|
|
|
|
|
{
|
|
|
|
|
switch (ev)
|
|
|
|
|
{
|
|
|
|
|
case WSREvent.CONNECT:
|
|
|
|
|
currentWebSockets.Add(webSocket);
|
|
|
|
|
break;
|
|
|
|
|
case WSREvent.CLOSE:
|
|
|
|
|
currentWebSockets.Remove(webSocket);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 14:15:10 +02:00
|
|
|
|
public void Broadcast(JSONObject json)
|
2019-08-03 12:57:32 +02:00
|
|
|
|
{
|
|
|
|
|
Broadcast(json.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Broadcast(String text)
|
|
|
|
|
{
|
|
|
|
|
foreach (WebSocket webSocket in CurrentWebSockets)
|
|
|
|
|
{
|
|
|
|
|
webSocket.Send(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void MessageReceived(WebSocketResourceRequestContext requestContext, string textMessage)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-19 14:15:10 +02:00
|
|
|
|
JSONObject json = (JSONObject)JSONParser.Parse(textMessage);
|
|
|
|
|
RPCCall rpcCall = json.ToObject<RPCCall>();
|
2019-08-03 12:57:32 +02:00
|
|
|
|
|
|
|
|
|
RPCResult rpcResult = SkyScannerService.RPCContainer.Invoke(rpcCall);
|
|
|
|
|
|
|
|
|
|
requestContext.WebSocket.Send(
|
2019-08-19 14:15:10 +02:00
|
|
|
|
JSONObject.From(rpcResult).ToString()
|
2019-08-03 12:57:32 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logging.Log(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|