private async void Socket_OnOpen(AppWebSocket sender, string roomId, bool isClientSocket)
        {
            var socketsReadyMessage =
                new WebSocketMessage()
            {
                type = "sockets-ready", roomId = roomId, socketId = sender.WebSocketId
            };
            var serializedMessage = JsonConvert.SerializeObject(socketsReadyMessage);

            if (!_activeRooms.ContainsKey(roomId))
            {
                if (isClientSocket)
                {
                    await sender.Send(new BasicMessage { message = "You have no access to the room", type = "error" }
                                      .ToJsonString());

                    return;
                }
                else
                {
                    _activeRooms.TryAdd(roomId, new WebSocketRoom(roomId, sender));
                }
            }
            else if (isClientSocket)
            {
                await _activeRooms[roomId].AddClientToRoom(sender);
            }
            else
            {
                _activeRooms[roomId].AddHostToRoom(sender);
            }

            await sender.Send(serializedMessage);
        }
 public override async Task handleNewSocket(AppWebSocket socket)
 {
     socket.OnMessageReceived += handleNewMessage;
     socket.OnOpen            += Socket_OnOpen;
     socket.OnClose           += Socket_OnClose;
     await socket.Initialize();
 }
Пример #3
0
 public void AddHostToRoom(AppWebSocket webSocket)
 {
     _hostWebSocket = webSocket;
     SendMessageToParticipants(new BasicMessage {
         message = "Host connected to the room", type = "warning"
     }
                               .ToJsonString());
 }
Пример #4
0
 public async Task HandleSendingMessage(AppWebSocket sender, string message)
 {
     if (sender == _hostWebSocket)
     {
         SendMessageToParticipants(message);
     }
     else
     {
         await SendMessageToHost(message);
     }
 }
Пример #5
0
        public async Task AddClientToRoom(AppWebSocket clientWebSocket)
        {
            _clientWebSockets.Add(clientWebSocket);
            var jObject = new JObject
            {
                ["type"]     = "new-client",
                ["roomId"]   = _id,
                ["clientId"] = clientWebSocket.WebSocketId
            };
            var serializedJson = JsonConvert.SerializeObject(jObject);

            await SendMessageToHost(serializedJson);
        }
Пример #6
0
 public void RemoveSocketFromRoom(AppWebSocket socket)
 {
     if (socket == _hostWebSocket)
     {
         _hostWebSocket = null;
         SendMessageToParticipants(new BasicMessage {
             message = "Host left the room", type = "warning"
         }
                                   .ToJsonString());
     }
     else
     {
         _clientWebSockets.Remove(socket);
     }
 }
 public AppWebSocketServer(IApplicationBuilder app, string Location)
 {
     app.Use(async(context, next) =>
     {
         if (context.Request.Path == Location)
         {
             if (context.WebSockets.IsWebSocketRequest)
             {
                 WebSocket webSocket       = await context.WebSockets.AcceptWebSocketAsync();
                 AppWebSocket appWebSocket = new AppWebSocket(webSocket);
                 await handleNewSocket(appWebSocket);
             }
             else
             {
                 context.Response.StatusCode = 400;
             }
         }
         else
         {
             await next();
         }
     });
 }
 public abstract void handleNewMessage(AppWebSocket socket, string message);
 public abstract void handleSocketClose(AppWebSocket socket);
 public abstract Task handleNewSocket(AppWebSocket socket);
Пример #11
0
 public WebSocketRoom(string id, AppWebSocket host)
 {
     _id               = id;
     _hostWebSocket    = host;
     _clientWebSockets = new List <AppWebSocket>();
 }
        public override async void handleNewMessage(AppWebSocket socket, string message)
        {
            var parsedMessage = JsonConvert.DeserializeObject <WebSocketMessage>(message);
            //WORKAROUND to fix problem with end of message
            var messageToSend = JsonConvert.SerializeObject(parsedMessage);

            if (_activeRooms.ContainsKey(parsedMessage.roomId))
            {
                var room = _activeRooms[parsedMessage.roomId];
                if (parsedMessage.type == "discussion")
                {
                    await room.HandleSendingMessage(socket, messageToSend);

                    if (!(parsedMessage.content["estimates"] is JArray estimates) || estimates.Count <= 1)
                    {
                        return;
                    }
                    var estimatesList = estimates.ToObject <List <WsEstimate> >()
                                        .OrderBy(e => e.estimate)
                                        .ToList();
                    var minEstimate = estimatesList.First();
                    var maxEstimate = estimatesList.Last();

                    var startDiscussionMessage =
                        new WebSocketMessage()
                    {
                        roomId   = parsedMessage.roomId,
                        type     = "start-discussion",
                        socketId = parsedMessage.socketId
                    };
                    var serializedDisussionMessage = JsonConvert.SerializeObject(startDiscussionMessage);

                    var minEstimates = estimatesList.Where(e => e.estimate == minEstimate.estimate).ToList();
                    minEstimates.ForEach(async e =>
                    {
                        try
                        {
                            await room.SendMessageToParticipant(e.socketId, serializedDisussionMessage);
                        }
                        catch (InvalidOperationException ex)
                        {
                            var errorString = "Error sending message to " + e.socketId + "; Socket not found." +
                                              ex.Message;
                            var errorMessage = new BasicMessage()
                            {
                                message = errorString, type = "error"
                            };
                            await socket.Send(errorMessage.ToJsonString());
                        }
                    });
                    if (minEstimate.estimate != maxEstimate.estimate)
                    {
                        var maxEstimates = estimatesList.Where(e => e.estimate == maxEstimate.estimate).ToList();
                        maxEstimates.ForEach(async e =>
                        {
                            try
                            {
                                await room.SendMessageToParticipant(e.socketId, serializedDisussionMessage);
                            }
                            catch (InvalidOperationException ex)
                            {
                                var errorString = "Error sending message to " + e.socketId + "; Socket not found." +
                                                  ex.Message;
                                var errorMessage = new BasicMessage()
                                {
                                    message = errorString, type = "error"
                                };
                                await socket.Send(errorMessage.ToJsonString());
                            }
                        }
                                             );
                    }
                }
                else if (parsedMessage.type == "chat")
                {
                    await room.SendMessageToOthers(socket.WebSocketId, messageToSend);
                }
                else
                {
                    try
                    {
                        await _activeRooms[parsedMessage.roomId].HandleSendingMessage(socket, messageToSend);
                    }
                    catch (WebSocketException ex)
                    {
                        await socket.Send(new BasicMessage { message = ex.Message, type = "error" }.ToJsonString());
                    }
                }
            }
            else
            {
                await socket.Send(new BasicMessage { message = "Room not found", type = "error" }.ToJsonString());
            }
        }
 public override void handleSocketClose(AppWebSocket socket)
 {
 }
 private void Socket_OnClose(AppWebSocket sender, string roomId)
 {
     _activeRooms[roomId].RemoveSocketFromRoom(sender);
 }