public async Task Invoke(HttpContext context, IClientRegistryService clientRegistry) { if (!context.WebSockets.IsWebSocketRequest) { await _next(context); return; } var webSocket = await context.WebSockets.AcceptWebSocketAsync(); _socketService.AddConnection(webSocket); while (webSocket.State == WebSocketState.Open) { var buffer = new ArraySegment <Byte>(new Byte[1024]); var received = await webSocket.ReceiveAsync(buffer, CancellationToken.None); switch (received.MessageType) { case WebSocketMessageType.Text: var content = Encoding.UTF8.GetString(buffer); if (content.StartsWith("username")) { // TODO: Do this during the login var username = content.Split(":")[1].Replace("\0", ""); _socketService.AddName(webSocket, username); clientRegistry.FireRegister(Startup.OwnHost, username); } continue; case WebSocketMessageType.Close: await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); _socketService.RemoveConnection(webSocket); return; } } }
private static void Handle(string message, string senderName, ILogger <Startup> logger, IClientRegistryService clientRegister, IMessageSender messageSender) { var tt = JsonConvert.DeserializeObject <Message>(message); logger.LogInformation($"FireRegister - Server: {Startup.OwnHost} User: {tt.Receiver}"); // TODO: Same user in multiple server clientRegister.FireRegister(Startup.OwnHost, senderName); messageSender.Send(message); }