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 () {
|
||||
|
||||
// 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...");
|
||||
|
||||
};
|
||||
|
@ -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()
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
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
|
||||
{
|
||||
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…
x
Reference in New Issue
Block a user