Browse Source

Make Modules able to ws communicate

master
amki 5 years ago
parent
commit
f6fc258f57
  1. BIN
      DiscoBot/98293701175955456-rss.db
  2. 2
      DiscoBot/ClientApp/src/app/calendar/calendar.component.ts
  3. 44
      DiscoBot/DisBot.cs
  4. 1
      DiscoBot/IModule.cs
  5. 1
      DiscoBot/calendar/Calendar.cs
  6. 14
      DiscoBot/calendar/JSONObjects.cs
  7. 1
      DiscoBot/rss/Rss.cs

BIN
DiscoBot/98293701175955456-rss.db

Binary file not shown.

2
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...");
};

44
DiscoBot/DisBot.cs

@ -20,7 +20,6 @@ namespace DiscoBot
public class DisBot
{
private DiscordSocketClient discordClient;
private List<WebSocket> webSockets = new List<WebSocket>();
private WSController wsC;
private List<Type> moduleTypes = new List<Type>();
private Dictionary<ulong, List<IModule>> guildModules = new Dictionary<ulong, List<IModule>>();
@ -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<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);
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
webSockets.Add(webSocket);
ArraySegment<Byte> buffer = new ArraySegment<byte>(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<Login>(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<string> ReadOneStringFromWebSocket(WebSocket ws)
{
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()

1
DiscoBot/IModule.cs

@ -10,6 +10,7 @@ namespace DiscoBot
interface IModule
{
Dictionary<string,Func<SocketMessage, string[], Task>> Commands { get; set; }
string Name { get; set; }
void Initialize();
void OnNewWebSocket(WebSocket ws);
}

1
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<WebSocket> webSockets = new List<WebSocket>();
public enum Attendance { Attending, Maybe }

14
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; }
}
}

1
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<string, Timer> timers = new Dictionary<string, Timer>();

Loading…
Cancel
Save