diff --git a/DiscoBot/98293701175955456-rss.db b/DiscoBot/98293701175955456-rss.db index 1b92d88..5e70b8d 100644 Binary files a/DiscoBot/98293701175955456-rss.db and b/DiscoBot/98293701175955456-rss.db differ diff --git a/DiscoBot/ClientApp/src/app/calendar/calendar.component.ts b/DiscoBot/ClientApp/src/app/calendar/calendar.component.ts index 8644e45..2c78f82 100644 --- a/DiscoBot/ClientApp/src/app/calendar/calendar.component.ts +++ b/DiscoBot/ClientApp/src/app/calendar/calendar.component.ts @@ -15,7 +15,7 @@ export class CalendarComponent { ws.onopen = function () { // Web Socket is connected, send data using send() - ws.send("Message to send"); + ws.send('{op:"connect", gid: 98293701175955456, module:"calendar"}'); alert("Message is sent..."); }; diff --git a/DiscoBot/DisBot.cs b/DiscoBot/DisBot.cs index f21d1f2..71060d1 100644 --- a/DiscoBot/DisBot.cs +++ b/DiscoBot/DisBot.cs @@ -20,7 +20,6 @@ namespace DiscoBot public class DisBot { private DiscordSocketClient discordClient; - private List webSockets = new List(); private WSController wsC; private List moduleTypes = new List(); private Dictionary> guildModules = new Dictionary>(); @@ -40,7 +39,7 @@ namespace DiscoBot // WebSocket an richtiges module zustellen while (!result.CloseStatus.HasValue) { - var msgBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(calendarContext.CalendarItems)); + var msgBytes = Encoding.UTF8.GetBytes("penis"); Console.WriteLine(msgBytes); await webSocket.SendAsync(new ArraySegment(msgBytes, 0, msgBytes.Length), result.MessageType, result.EndOfMessage, CancellationToken.None); @@ -53,35 +52,26 @@ namespace DiscoBot { Console.WriteLine("Handling Websocket to "+ context.Request.Path); WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); - webSockets.Add(webSocket); - ArraySegment buffer = new ArraySegment(new Byte[8192]); - WebSocketReceiveResult result = await webSocket.ReceiveAsync(buffer, CancellationToken.None); - // {op:"connect", gid: 324984732895, module:"calendar"} - using (var ms = new MemoryStream()){ - do + string msg = await DisBot.ReadOneStringFromWebSocket(webSocket); + var login = JsonConvert.DeserializeObject(msg); + var modules = guildModules[login.Gid]; + foreach (var m in modules) + { + if (m.Name == login.Module) { - ms.Write(buffer.Array, buffer.Offset, result.Count); - } - while (!result.EndOfMessage); - - ms.Seek(0, SeekOrigin.Begin); - - if(result.MessageType == WebSocketMessageType.Text) - { - using (var reader = new StreamReader(ms, Encoding.UTF8)) - { - string line; - while((line = reader.ReadLine()) != null) - { - Console.WriteLine(line); - } - } + m.OnNewWebSocket(webSocket); } } + } - //IModule module = guildModules[gid]; - //module.OnNewWebSocket(webSocket); - await Echo(context, webSocket); + public static async Task ReadOneStringFromWebSocket(WebSocket ws) + { + ArraySegment buffer = new ArraySegment(new Byte[8192]); + WebSocketReceiveResult result = await ws.ReceiveAsync(buffer, CancellationToken.None); + var buf = buffer.ToArray(); + string line = System.Text.Encoding.UTF8.GetString(buf, 0, result.Count); + Console.WriteLine("READ: " + line + "...:::..."); + return line; } public async void Initialize() diff --git a/DiscoBot/IModule.cs b/DiscoBot/IModule.cs index d269826..b36078d 100644 --- a/DiscoBot/IModule.cs +++ b/DiscoBot/IModule.cs @@ -10,6 +10,7 @@ namespace DiscoBot interface IModule { Dictionary> Commands { get; set; } + string Name { get; set; } void Initialize(); void OnNewWebSocket(WebSocket ws); } diff --git a/DiscoBot/calendar/Calendar.cs b/DiscoBot/calendar/Calendar.cs index e708d79..87785e0 100644 --- a/DiscoBot/calendar/Calendar.cs +++ b/DiscoBot/calendar/Calendar.cs @@ -10,6 +10,7 @@ namespace DiscoBot.calendar { public class Calendar : IModule { + string IModule.Name { get => "calendar"; set => throw new NotImplementedException(); } private SocketGuild guild; private List webSockets = new List(); public enum Attendance { Attending, Maybe } diff --git a/DiscoBot/calendar/JSONObjects.cs b/DiscoBot/calendar/JSONObjects.cs new file mode 100644 index 0000000..d2e4619 --- /dev/null +++ b/DiscoBot/calendar/JSONObjects.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DiscoBot.calendar +{ + public class Login { + // {op:"connect", gid: 324984732895, module:"calendar"} + public string Op { get; set; } + public ulong Gid { get; set; } + public string Module { get; set; } + } +} diff --git a/DiscoBot/rss/Rss.cs b/DiscoBot/rss/Rss.cs index 820183b..3aea0de 100644 --- a/DiscoBot/rss/Rss.cs +++ b/DiscoBot/rss/Rss.cs @@ -13,6 +13,7 @@ namespace DiscoBot.rss { public class Rss : IModule { + string IModule.Name { get => "Rss"; set => throw new NotImplementedException(); } private SocketGuild guild; private RssContext rssContext; private Dictionary timers = new Dictionary();