Make Modules able to ws communicate
This commit is contained in:
		
							parent
							
								
									461fdada73
								
							
						
					
					
						commit
						f6fc258f57
					
				
										
											Binary file not shown.
										
									
								
							| @ -15,7 +15,7 @@ export class CalendarComponent { | |||||||
|     ws.onopen = function () { |     ws.onopen = function () { | ||||||
| 
 | 
 | ||||||
|       // Web Socket is connected, send data using send()
 |       // 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...");  |       alert("Message is sent...");  | ||||||
| 
 | 
 | ||||||
|     }; |     }; | ||||||
|  | |||||||
| @ -20,7 +20,6 @@ namespace DiscoBot | |||||||
|     public class DisBot |     public class DisBot | ||||||
|     { |     { | ||||||
|         private DiscordSocketClient discordClient; |         private DiscordSocketClient discordClient; | ||||||
|         private List<WebSocket> webSockets = new List<WebSocket>(); |  | ||||||
|         private WSController wsC; |         private WSController wsC; | ||||||
|         private List<Type> moduleTypes = new List<Type>(); |         private List<Type> moduleTypes = new List<Type>(); | ||||||
|         private Dictionary<ulong, List<IModule>> guildModules = new Dictionary<ulong, List<IModule>>(); |         private Dictionary<ulong, List<IModule>> guildModules = new Dictionary<ulong, List<IModule>>(); | ||||||
| @ -40,7 +39,7 @@ namespace DiscoBot | |||||||
|             // WebSocket an richtiges module zustellen |             // WebSocket an richtiges module zustellen | ||||||
|             while (!result.CloseStatus.HasValue) |             while (!result.CloseStatus.HasValue) | ||||||
|             { |             { | ||||||
|                 var msgBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(calendarContext.CalendarItems)); |                 var msgBytes = Encoding.UTF8.GetBytes("penis"); | ||||||
|                 Console.WriteLine(msgBytes); |                 Console.WriteLine(msgBytes); | ||||||
|                 await webSocket.SendAsync(new ArraySegment<byte>(msgBytes, 0, msgBytes.Length), result.MessageType, result.EndOfMessage, CancellationToken.None); |                 await webSocket.SendAsync(new ArraySegment<byte>(msgBytes, 0, msgBytes.Length), result.MessageType, result.EndOfMessage, CancellationToken.None); | ||||||
| 
 | 
 | ||||||
| @ -53,35 +52,26 @@ namespace DiscoBot | |||||||
|         { |         { | ||||||
|             Console.WriteLine("Handling Websocket to "+ context.Request.Path); |             Console.WriteLine("Handling Websocket to "+ context.Request.Path); | ||||||
|             WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); |             WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); | ||||||
|             webSockets.Add(webSocket); |             string msg = await DisBot.ReadOneStringFromWebSocket(webSocket); | ||||||
|             ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]); |             var login = JsonConvert.DeserializeObject<Login>(msg); | ||||||
|             WebSocketReceiveResult result = await webSocket.ReceiveAsync(buffer, CancellationToken.None); |             var modules = guildModules[login.Gid]; | ||||||
|             // {op:"connect", gid: 324984732895, module:"calendar"} |             foreach (var m in modules) | ||||||
|             using (var ms = new MemoryStream()){ |             { | ||||||
|                 do |                 if (m.Name == login.Module) | ||||||
|                 { |                 { | ||||||
|                     ms.Write(buffer.Array, buffer.Offset, result.Count); |                     m.OnNewWebSocket(webSocket); | ||||||
|                 } |  | ||||||
|                 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); |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|             //IModule module = guildModules[gid]; |         public static async Task<string> ReadOneStringFromWebSocket(WebSocket ws) | ||||||
|             //module.OnNewWebSocket(webSocket); |         { | ||||||
|             await Echo(context, webSocket); |             ArraySegment<Byte> buffer = new ArraySegment<byte>(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() |         public async void Initialize() | ||||||
|  | |||||||
| @ -10,6 +10,7 @@ namespace DiscoBot | |||||||
|     interface IModule |     interface IModule | ||||||
|     { |     { | ||||||
|         Dictionary<string,Func<SocketMessage, string[], Task>> Commands { get; set; } |         Dictionary<string,Func<SocketMessage, string[], Task>> Commands { get; set; } | ||||||
|  |         string Name { get; set; } | ||||||
|         void Initialize(); |         void Initialize(); | ||||||
|         void OnNewWebSocket(WebSocket ws); |         void OnNewWebSocket(WebSocket ws); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -10,6 +10,7 @@ namespace DiscoBot.calendar | |||||||
| { | { | ||||||
|     public class Calendar : IModule |     public class Calendar : IModule | ||||||
|     { |     { | ||||||
|  |         string IModule.Name { get => "calendar"; set => throw new NotImplementedException(); } | ||||||
|         private SocketGuild guild; |         private SocketGuild guild; | ||||||
|         private List<WebSocket> webSockets = new List<WebSocket>(); |         private List<WebSocket> webSockets = new List<WebSocket>(); | ||||||
|         public enum Attendance { Attending, Maybe } |         public enum Attendance { Attending, Maybe } | ||||||
|  | |||||||
							
								
								
									
										14
									
								
								DiscoBot/calendar/JSONObjects.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								DiscoBot/calendar/JSONObjects.cs
									
									
									
									
									
										Normal file
									
								
							| @ -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; } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -13,6 +13,7 @@ namespace DiscoBot.rss | |||||||
| { | { | ||||||
|     public class Rss : IModule |     public class Rss : IModule | ||||||
|     { |     { | ||||||
|  |         string IModule.Name { get => "Rss"; set => throw new NotImplementedException(); } | ||||||
|         private SocketGuild guild; |         private SocketGuild guild; | ||||||
|         private RssContext rssContext; |         private RssContext rssContext; | ||||||
|         private Dictionary<string, Timer> timers = new Dictionary<string, Timer>(); |         private Dictionary<string, Timer> timers = new Dictionary<string, Timer>(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user