Implement dynamic channel list
This commit is contained in:
@@ -7,6 +7,11 @@ public struct SwitchChannel(string name)
|
||||
[JsonProperty("channel")] public string Name = name;
|
||||
}
|
||||
|
||||
public struct ChannelList(Dictionary<string, uint> channels)
|
||||
{
|
||||
[JsonProperty("channels")] public Dictionary<string, uint> Channels = channels;
|
||||
}
|
||||
|
||||
public struct Messages(MessageResponse[] set)
|
||||
{
|
||||
[JsonProperty("messages")] public MessageResponse[] Set = set;
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace ChatTwo.Http.MessageProtocol;
|
||||
|
||||
public class CloseEvent() : BaseEvent("close");
|
||||
|
||||
public class ChannelListEvent(ChannelList channelList) : BaseEvent("channel-list", JsonConvert.SerializeObject(channelList));
|
||||
|
||||
public class SwitchChannelEvent(SwitchChannel switchChannel) : BaseEvent("switch-channel", JsonConvert.SerializeObject(switchChannel));
|
||||
|
||||
public class NewMessageEvent(Messages messages) : BaseEvent("new-message", JsonConvert.SerializeObject(messages));
|
||||
|
||||
@@ -165,9 +165,11 @@ public class RouteController
|
||||
|
||||
// TODO Check if reconnect or new connection
|
||||
var messages = await WebserverUtil.FrameworkWrapper(Core.Processing.ReadMessageList);
|
||||
var channels = await Plugin.Framework.RunOnTick(Plugin.ChatLogWindow.GetAvailableChannels);
|
||||
var channelName = await Plugin.Framework.RunOnTick(() => Core.Processing.ReadChannelName(Plugin.ChatLogWindow.PreviousChannel));
|
||||
sse.OutboundQueue.Enqueue(new NewMessageEvent(new Messages(messages)));
|
||||
sse.OutboundQueue.Enqueue(new SwitchChannelEvent(new SwitchChannel(channelName)));
|
||||
sse.OutboundQueue.Enqueue(new ChannelListEvent(new ChannelList(channels.ToDictionary(pair => pair.Key, pair => (uint)pair.Value))));
|
||||
|
||||
await sse.HandleEventLoop(ctx);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using ChatTwo.Http.MessageProtocol;
|
||||
using ChatTwo.Code;
|
||||
using ChatTwo.Http.MessageProtocol;
|
||||
using WatsonWebserver.Core;
|
||||
using WatsonWebserver.Lite;
|
||||
using ExceptionEventArgs = WatsonWebserver.Core.ExceptionEventArgs;
|
||||
@@ -72,6 +73,24 @@ public class ServerCore : IAsyncDisposable
|
||||
Plugin.Log.Error(ex, "Sending channel switch over SSE failed.");
|
||||
}
|
||||
}
|
||||
|
||||
internal void SendChannelList()
|
||||
{
|
||||
try
|
||||
{
|
||||
Plugin.Framework.RunOnTick(() =>
|
||||
{
|
||||
var channels = Plugin.ChatLogWindow.GetAvailableChannels();
|
||||
var bundledResponse = new ChannelListEvent(new ChannelList(channels.ToDictionary(pair => pair.Key, pair => (uint)pair.Value)));
|
||||
foreach (var eventServer in EventConnections)
|
||||
eventServer.OutboundQueue.Enqueue(bundledResponse);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Plugin.Log.Error(ex, "Sending channel switch over SSE failed.");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region GeneralHandlers
|
||||
|
||||
@@ -13,12 +13,15 @@ class SSEConnection {
|
||||
});
|
||||
|
||||
this.socket.addEventListener('new-message', (event) => {
|
||||
let eventData = JSON.parse(event.data);
|
||||
for (let message of eventData.messages)
|
||||
for (let message of JSON.parse(event.data).messages)
|
||||
{
|
||||
addMessage(message);
|
||||
}
|
||||
});
|
||||
|
||||
this.socket.addEventListener('channel-list', (event) => {
|
||||
updateChannelOptions(JSON.parse(event.data).channels)
|
||||
});
|
||||
}
|
||||
|
||||
send(message) {
|
||||
@@ -31,14 +34,30 @@ const sse = new SSEConnection();
|
||||
|
||||
// channel switcher
|
||||
function updateChannelHint(label) {
|
||||
document.getElementById('channel-hint').innerText = label;
|
||||
document.getElementById('channel-hint').innerHTML = label;
|
||||
}
|
||||
|
||||
document.getElementById('channel-select').addEventListener('change', (event) => {
|
||||
updateChannelHint(event.target.value);
|
||||
// TODO: send new channel to "backend"
|
||||
// ws.send(...);
|
||||
});
|
||||
|
||||
function updateChannelOptions(channels) {
|
||||
let select = document.getElementById('channel-select');
|
||||
|
||||
// clear existing channels
|
||||
select.innerHTML = '';
|
||||
|
||||
for (let [ name, channel ] of Object.entries(channels))
|
||||
{
|
||||
let option = document.createElement('option');
|
||||
option.text = name;
|
||||
option.value = channel;
|
||||
|
||||
select.appendChild(option)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// functions for handling the message list
|
||||
function scrollMessagesToBottom() {
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user