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);
        }
Пример #2
0
        public async Task SendMessageToOthers(string senderId, string message)
        {
            if (senderId == _hostWebSocket.WebSocketId)
            {
                SendMessageToParticipants(message);
            }
            else
            {
                await _hostWebSocket.Send(message);

                _clientWebSockets.Where(ws => ws.WebSocketId != senderId)
                .ToList()
                .ForEach(async ws => await ws.Send(message));
            }
        }
        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());
            }
        }