implement route for channel switching and use json for received data
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace ChatTwo.Http.MessageProtocol;
|
namespace ChatTwo.Http.MessageProtocol;
|
||||||
|
|
||||||
|
#region Outgoing
|
||||||
public struct SwitchChannel(string name)
|
public struct SwitchChannel(string name)
|
||||||
{
|
{
|
||||||
[JsonProperty("channel")] public string Name = name;
|
[JsonProperty("channel")] public string Name = name;
|
||||||
@@ -22,3 +23,16 @@ public struct MessageResponse()
|
|||||||
[JsonProperty("timestamp")] public string Timestamp = "";
|
[JsonProperty("timestamp")] public string Timestamp = "";
|
||||||
[JsonProperty("messageHTML")] public string Message = "";
|
[JsonProperty("messageHTML")] public string Message = "";
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Incoming
|
||||||
|
public struct IncomingMessage()
|
||||||
|
{
|
||||||
|
[JsonProperty("message")] public string Message = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct IncomingChannel()
|
||||||
|
{
|
||||||
|
[JsonProperty("channel")] public uint Channel = uint.MaxValue;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
|
using ChatTwo.Code;
|
||||||
using ChatTwo.Http.MessageProtocol;
|
using ChatTwo.Http.MessageProtocol;
|
||||||
using ChatTwo.Util;
|
using ChatTwo.Util;
|
||||||
using Lumina.Data.Files;
|
using Lumina.Data.Files;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using WatsonWebserver.Core;
|
using WatsonWebserver.Core;
|
||||||
|
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||||
using HttpMethod = WatsonWebserver.Core.HttpMethod;
|
using HttpMethod = WatsonWebserver.Core.HttpMethod;
|
||||||
|
|
||||||
namespace ChatTwo.Http;
|
namespace ChatTwo.Http;
|
||||||
@@ -20,6 +22,11 @@ public class RouteController
|
|||||||
private readonly ConcurrentDictionary<string, long> RateLimit = [];
|
private readonly ConcurrentDictionary<string, long> RateLimit = [];
|
||||||
internal readonly ConcurrentDictionary<string, bool> SessionTokens = [];
|
internal readonly ConcurrentDictionary<string, bool> SessionTokens = [];
|
||||||
|
|
||||||
|
private readonly JsonSerializerSettings JsonSettings = new()
|
||||||
|
{
|
||||||
|
Error = delegate(object? sender, ErrorEventArgs args) { args.ErrorContext.Handled = true; }
|
||||||
|
};
|
||||||
|
|
||||||
public RouteController(Plugin plugin, ServerCore core)
|
public RouteController(Plugin plugin, ServerCore core)
|
||||||
{
|
{
|
||||||
Plugin = plugin;
|
Plugin = plugin;
|
||||||
@@ -40,7 +47,7 @@ public class RouteController
|
|||||||
// Post Auth
|
// Post Auth
|
||||||
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.GET, "/chat", ChatBoxRoute, ExceptionRoute);
|
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.GET, "/chat", ChatBoxRoute, ExceptionRoute);
|
||||||
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.POST, "/send", ReceiveMessage, ExceptionRoute);
|
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.POST, "/send", ReceiveMessage, ExceptionRoute);
|
||||||
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.POST, "/switch", ReceiveChannelSwitch, ExceptionRoute);
|
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.POST, "/channel", ReceiveChannelSwitch, ExceptionRoute);
|
||||||
|
|
||||||
// Server-Sent Events Route
|
// Server-Sent Events Route
|
||||||
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.GET, "/sse", NewSSEConnection, ExceptionRoute);
|
Core.HostContext.Routes.PostAuthentication.Static.Add(HttpMethod.GET, "/sse", NewSSEConnection, ExceptionRoute);
|
||||||
@@ -141,16 +148,22 @@ public class RouteController
|
|||||||
|
|
||||||
private async Task ReceiveMessage(HttpContextBase ctx)
|
private async Task ReceiveMessage(HttpContextBase ctx)
|
||||||
{
|
{
|
||||||
var content = ctx.Request.DataAsString;
|
if (ctx.Request.ContentType != "application/json")
|
||||||
if (content.Length is > 500 or < 2)
|
|
||||||
{
|
{
|
||||||
await ctx.Response.Send("Invalid length for a chat message received.");
|
await ctx.Response.Send("Request contains wrong content type.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var content = JsonConvert.DeserializeObject<IncomingMessage>(ctx.Request.DataAsString, JsonSettings);
|
||||||
|
if (content.Message.Length is < 2 or > 500)
|
||||||
|
{
|
||||||
|
await ctx.Response.Send("Invalid message received.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Plugin.Framework.RunOnFrameworkThread(() =>
|
await Plugin.Framework.RunOnFrameworkThread(() =>
|
||||||
{
|
{
|
||||||
Plugin.ChatLogWindow.Chat = content;
|
Plugin.ChatLogWindow.Chat = content.Message;
|
||||||
Plugin.ChatLogWindow.SendChatBox(Plugin.ChatLogWindow.CurrentTab);
|
Plugin.ChatLogWindow.SendChatBox(Plugin.ChatLogWindow.CurrentTab);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -159,20 +172,25 @@ public class RouteController
|
|||||||
|
|
||||||
private async Task ReceiveChannelSwitch(HttpContextBase ctx)
|
private async Task ReceiveChannelSwitch(HttpContextBase ctx)
|
||||||
{
|
{
|
||||||
var content = ctx.Request.DataAsString;
|
if (ctx.Request.ContentType != "application/json")
|
||||||
if (content.Length is > 500 or < 2)
|
|
||||||
{
|
{
|
||||||
await ctx.Response.Send("Invalid length for a chat message received.");
|
await ctx.Response.Send("Request contains wrong content type.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var channel = JsonConvert.DeserializeObject<IncomingChannel>(ctx.Request.DataAsString, JsonSettings);
|
||||||
|
if (!Enum.IsDefined(typeof(InputChannel), channel.Channel))
|
||||||
|
{
|
||||||
|
await ctx.Response.Send("Invalid channel received.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Plugin.Framework.RunOnFrameworkThread(() =>
|
await Plugin.Framework.RunOnFrameworkThread(() =>
|
||||||
{
|
{
|
||||||
Plugin.ChatLogWindow.Chat = content;
|
Plugin.ChatLogWindow.SetChannel((InputChannel)channel.Channel);
|
||||||
Plugin.ChatLogWindow.SendChatBox(Plugin.ChatLogWindow.CurrentTab);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await ctx.Response.Send("Message was send to the channel.");
|
await ctx.Response.Send("Function to switch channels has been called.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task NewSSEConnection(HttpContextBase ctx)
|
private async Task NewSSEConnection(HttpContextBase ctx)
|
||||||
|
|||||||
@@ -38,8 +38,20 @@ function updateChannelHint(label) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('channel-select').addEventListener('change', (event) => {
|
document.getElementById('channel-select').addEventListener('change', (event) => {
|
||||||
// TODO: send new channel to "backend"
|
(async () => {
|
||||||
// ws.send(...);
|
const rawResponse = await fetch('/channel', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({channel: event.target.value})
|
||||||
|
});
|
||||||
|
const content = await rawResponse.json();
|
||||||
|
|
||||||
|
// TODO use the response
|
||||||
|
console.log(content);
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
function updateChannelOptions(channels) {
|
function updateChannelOptions(channels) {
|
||||||
@@ -122,6 +134,21 @@ document.querySelector('#input > form').addEventListener('submit', (event) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const rawResponse = await fetch('/send', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({message: message})
|
||||||
|
});
|
||||||
|
const content = await rawResponse.json();
|
||||||
|
|
||||||
|
// TODO use the response
|
||||||
|
console.log(content);
|
||||||
|
})();
|
||||||
|
|
||||||
chatInput.value = '';
|
chatInput.value = '';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user