Example #1
0
        private async Task OnDisconnected(DisconnectedArgs args)
        {
            ChatUserInfoDto disconnectingInfo = null;

            _connectedUsers.TryRemove(args.Peer, out disconnectingInfo);

            if (disconnectingInfo == null)
            {
                throw new InvalidOperationException("Client doesn't connected.");
            }

            disconnectingInfo.Status = UserStatusDto.Disconnected;

            _scene.Broadcast("statuschange", (s) => {
                args.Peer.Serializer().Serialize((int)UserStatusDto.Disconnected, s);
                args.Peer.Serializer().Serialize(disconnectingInfo, s);
            });

            ChatDisconnectedContext chatDisconnected = new ChatDisconnectedContext(disconnectingInfo.UserId);
            await _chatEventHandler?.RunEventHandler(eh =>
            {
                return(eh.OnDisconnected(chatDisconnected));
            }, ex =>
            {
                _log.Log(LogLevel.Error, _logCategory, "An error occured when the chat service try to dispose observable.", ex);
                throw new ClientException($"An error occured when the chat service try to dispose observable.");
            });
        }
Example #2
0
        public async Task OnMessageReceived(IScenePeerClient client, string text, Action <ChatMessageDto, DestinationType> sender)
        {
            // Check if user is connected
            ChatUserInfoDto userInfo = null;

            _connectedUsers.TryGetValue(client, out userInfo);
            if (userInfo == null)
            {
                throw new ClientException($"User not found {client.Id} .");
            }

            // Store message in log
            ChatMessage messageLog = new ChatMessage
            {
                Channel  = client.SceneId,
                Date     = DateTime.UtcNow,
                Message  = text,
                Metadata = new JObject(),
                UserInfo = new UserInfo
                {
                    UserId = userInfo.UserId,
                    Data   = JObject.Parse(userInfo.Data)
                }
            };

            ChatMessageDto messageDto = new ChatMessageDto
            {
                Message   = text,
                TimeStamp = _env.Clock,
                UserInfo  = userInfo,
            };

            ChatMsgReceivedContext receiveCtx = new ChatMsgReceivedContext(messageLog, messageDto);

            receiveCtx.Destination = DestinationType.All;

            await _chatEventHandler?.RunEventHandler(eh =>
            {
                return(eh.OnMessageReceived(receiveCtx));
            }, ex =>
            {
                _log.Log(LogLevel.Error, _logCategory, "An error occured when the chat service check if the user is in jail.", ex);
                throw new ClientException($"An error occured when the chat service check if the user is in jail.");
            });

            _chatRepository.AddMessageLog(receiveCtx.Message);
            _messagesSource.OnNext(receiveCtx.Message);
            sender(receiveCtx.MessageDto, receiveCtx.Destination);
        }
Example #3
0
        // Todo jojo : vérifier avec JN ou JM comment faire pour la gestion des ban et des erreurs lors du get info
        // Pour ne pas ajouter un utilisatuer alors qu'il est déjà connecté
        // Todo jojo : ajouter une métadata pour mettre la/les raisons de l'erreur
        private async Task OnConnected(IScenePeerClient clientPeer)
        {
            ChatUserInfoDto connectingUserInfo = new ChatUserInfoDto {
                PeerId = clientPeer.Id
            };

            ChatUserInfoCtx chatCtx = new ChatUserInfoCtx {
                PeerId = clientPeer.Id
            };

            await _eventUserHandlers?.RunEventHandler(eh => eh.GetUserInfo(chatCtx), ex =>
            {
                _log.Log(LogLevel.Error, _logCategory, "An error occured while running ChatService.GetUserInfo event handlers", ex);
                connectingUserInfo = new ChatUserInfoDto {
                    PeerId = Convert.ToInt64(clientPeer.Id.ToString()), Status = UserStatusDto.Error
                };

                clientPeer.Send("statuschange", (s) => {
                    clientPeer.Serializer().Serialize((int)UserStatusDto.Error, s);
                    clientPeer.Serializer().Serialize(connectingUserInfo, s);
                },
                                PacketPriority.MEDIUM_PRIORITY,
                                PacketReliability.RELIABLE
                                );
            });

            //Send list of connected user
            if (!_connectedUsers.Any(x => x.Value.PeerId == connectingUserInfo.PeerId))
            {
                connectingUserInfo.UserId = chatCtx.User.Id;
                connectingUserInfo.Status = UserStatusDto.Connected;
                connectingUserInfo.Data   = chatCtx.User.UserData.ToString();

                _connectedUsers.TryAdd(clientPeer, connectingUserInfo);
                List <ChatUserInfoDto> usersInChat = _connectedUsers.Values.ToList <ChatUserInfoDto>();

                _scene.Broadcast("statuschange", (s) => {
                    clientPeer.Serializer().Serialize((int)UserStatusDto.Connected, s);
                    clientPeer.Serializer().Serialize(usersInChat, s);
                });
            }
        }