ln.http.api/ln.http.api/JSONWebSocketResponse.cs

39 lines
1.1 KiB
C#

using System;
using ln.http.websocket;
using ln.json;
using ln.logging;
namespace ln.http.api
{
public delegate void WebSocketReceivedJSON(JSONWebSocketResponse sender, JSONValue json);
public class JSONWebSocketResponse : WebSocketResponse
{
public event WebSocketReceivedJSON OnWebSocketReceivedJSON;
public JSONWebSocketResponse(){ }
public override void Received(string textMessage)
{
base.Received(textMessage);
try
{
JSONValue json = JSONParser.Parse(textMessage);
try {
OnWebSocketReceivedJSON?.Invoke(this, json);
} catch (Exception ex)
{
Logging.Log(ex);
}
} catch (Exception e)
{
Logging.Log(LogLevel.ERROR, "JSONWebSocketResponse: received unparsable json message: {0}", textMessage);
Close();
}
}
public void Send(JSONValue json) => Send(json.ToString());
}
}