From f6fc258f57892eb014da5962ef6573379d1dbc78 Mon Sep 17 00:00:00 2001 From: amki Date: Tue, 4 Jun 2019 21:56:48 +0200 Subject: [PATCH] Make Modules able to ws communicate --- DiscoBot/98293701175955456-rss.db | Bin 20480 -> 20480 bytes .../src/app/calendar/calendar.component.ts | 2 +- DiscoBot/DisBot.cs | 44 +++++++----------- DiscoBot/IModule.cs | 1 + DiscoBot/calendar/Calendar.cs | 1 + DiscoBot/calendar/JSONObjects.cs | 14 ++++++ DiscoBot/rss/Rss.cs | 1 + 7 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 DiscoBot/calendar/JSONObjects.cs diff --git a/DiscoBot/98293701175955456-rss.db b/DiscoBot/98293701175955456-rss.db index 1b92d88ec0d0cf22807e7680aeab8c0b5d922a63..5e70b8d499774514f37d2bc879713e2eed4ce7cc 100644 GIT binary patch delta 216 zcmZozz}T>Wae_4Cvxzd!jL$YEY_{Sx(={+rFto5THn%b~nXF(P01`1$Ffy_-Ftjo< zom^|30TD4YwK4?>PX1*5kk?e#$Up%oVgl4?G5LW_0n{9GD-#1NQ_IP*wsFD`lg+J+ zjIB(}v<(cb3=AfplMRF0Xap89w5vdf0Btmxyv;5^9LZHC1`xdr3=GwiW#uoxRU4XF M8JchYWzQx606<7KmH+?% delta 214 zcmXxeF$%&!5Czbru@OPAu+ZT4BFxV2=*mHSJ3_CtXP7Xes5B4a6apJ$0xDnJn9h zywg|6kV4JIvnDC0yB3W=EA7~NMv~QR-KhkXhR`A$0x*C)l4TReb 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();