Add SendMessage with automatic json serialization in ConnectionHandler

This commit is contained in:
ChronosX88 2020-10-09 17:12:02 +04:00
parent dc030c0521
commit ce8ed0d7ad
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 10 additions and 10 deletions

View File

@ -82,6 +82,11 @@ namespace Zirconium.Core
this.Send(message); this.Send(message);
} }
public void SendMessage(BaseMessage message)
{
this.Send(JsonConvert.SerializeObject(message));
}
public void CloseConnection() { public void CloseConnection() {
this.Sessions.CloseSession(this.ID); // TODO need to clarify if CloseSession also calls OnClose callback this.Sessions.CloseSession(this.ID); // TODO need to clarify if CloseSession also calls OnClose callback
} }

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json; using Newtonsoft.Json;
using Zirconium.Core.Logging;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Utils; using Zirconium.Utils;
@ -98,8 +97,7 @@ namespace InBandLogin
p.DeviceID = deviceID; p.DeviceID = deviceID;
reply.Payload = p.ToDictionary(); reply.Payload = p.ToDictionary();
reply.Ok = true; reply.Ok = true;
var msg = JsonConvert.SerializeObject(reply); session.ConnectionHandler.SendMessage(reply);
session.ConnectionHandler.SendMessage(msg);
} }
else else
{ {
@ -108,9 +106,8 @@ namespace InBandLogin
errID, errID,
"Username/password isn't valid", "Username/password isn't valid",
new Dictionary<string, object>() new Dictionary<string, object>()
).ToDictionary(); );
var msg = JsonConvert.SerializeObject(reply); session.ConnectionHandler.SendMessage(reply);
session.ConnectionHandler.SendMessage(msg);
} }
} }
@ -170,8 +167,7 @@ namespace InBandLogin
new Dictionary<string, object>() new Dictionary<string, object>()
); );
} }
var errorMsg = JsonConvert.SerializeObject(errorReply); session.ConnectionHandler.SendMessage(errorReply);
session.ConnectionHandler.SendMessage(errorMsg);
return; return;
} }
BaseMessage reply = new BaseMessage(message, true); BaseMessage reply = new BaseMessage(message, true);
@ -185,8 +181,7 @@ namespace InBandLogin
} }
reply.Payload = p.ToDictionary(); reply.Payload = p.ToDictionary();
reply.Ok = true; reply.Ok = true;
var msg = JsonConvert.SerializeObject(reply); session.ConnectionHandler.SendMessage(reply);
session.ConnectionHandler.SendMessage(msg);
} }
public bool IsAuthorizationRequired() public bool IsAuthorizationRequired()