|
|
@ -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() |
|
|
|