Beispiel #1
0
        private void OnMessage(object sender, MessageEventArgs e)
        {
            if (!e.IsText)
            {
                return;
            }

            var container = JsonConvert.DeserializeObject <MessageContainer>(e.Data);

            switch (container.Identifier)
            {
            case nameof(ConnectionResponse):
                var connectionResponse = ((JObject)container.Payload).ToObject(typeof(ConnectionResponse)) as ConnectionResponse;
                if (connectionResponse.Result == ResultCodes.Failure)
                {
                    Login = string.Empty;
                    string source = string.Empty;
                    ErrorReceived?.Invoke(this, new ErrorReceivedEventArgs(connectionResponse.Reason, connectionResponse.Date));
                }
                if (!String.IsNullOrEmpty(Login))
                {
                    ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(Login, DateTime.Now, true, connectionResponse.OnlineClients));
                }
                break;

            case nameof(ConnectionBroadcast):
                var connectionBroadcast = ((JObject)container.Payload).ToObject(typeof(ConnectionBroadcast)) as ConnectionBroadcast;
                if (connectionBroadcast.Login != Login)
                {
                    ConnectionReceived?.Invoke(this, new ConnectionReceivedEventArgs(connectionBroadcast.Login, connectionBroadcast.IsConnected, connectionBroadcast.Date));
                }
                break;

            case nameof(MessageBroadcast):
                var messageBroadcast = ((JObject)container.Payload).ToObject(typeof(MessageBroadcast)) as MessageBroadcast;
                MessageReceived?.Invoke(this, new MessageReceivedEventArgs(messageBroadcast.Source, messageBroadcast.Target, messageBroadcast.Message, messageBroadcast.Date,
                                                                           messageBroadcast.GroupName));
                break;

            case nameof(ClientsListResponse):
                var clientsListResponse = ((JObject)container.Payload).ToObject(typeof(ClientsListResponse)) as ClientsListResponse;
                ClientsListReceived?.Invoke(this, new ClientsListReceivedEventArgs(clientsListResponse.Clients));
                break;

            case nameof(ChatHistoryResponse):
                var chatHistoryResponse = ((JObject)container.Payload).ToObject(typeof(ChatHistoryResponse)) as ChatHistoryResponse;
                ChatHistoryReceived?.Invoke(this, new ChatHistoryReceivedEventArgs(chatHistoryResponse.ClientMessages));
                break;

            case nameof(FilterResponse):
                var filterResponse = ((JObject)container.Payload).ToObject(typeof(FilterResponse)) as FilterResponse;
                FilteredMessagesReceived?.Invoke(this, new FilteredMessagesReceivedEventArgs(filterResponse.FilteredMessages));
                break;

            case nameof(GroupsListResponse):
                var groupsListResponse = ((JObject)container.Payload).ToObject(typeof(GroupsListResponse)) as GroupsListResponse;
                GroupsReceived?.Invoke(this, new GroupsReceivedEventArgs(groupsListResponse.Groups));
                break;

            case nameof(GroupBroadcast):
                var groupBroadcast = ((JObject)container.Payload).ToObject(typeof(GroupBroadcast)) as GroupBroadcast;
                GroupsReceived?.Invoke(this, new GroupsReceivedEventArgs(groupBroadcast.Group));
                break;
            }
        }
Beispiel #2
0
        public void RequestMessageHistory()
        {
            if (!IsAuthenticated)
            {
                throw new InvalidOperationException("Not authenticated. Request/set token first.");
            }

            if (AccountData == null)
            {
                throw new InvalidOperationException("No account data requested. Request account data first.");
            }

            var jObject = new JObject();

            jObject["chat_token"] = Token;
            jObject["usernames"]  = JArray.FromObject(AccountData.Users.Where(x => x.Update).Select(x => x.Name).ToArray());

            try
            {
                var response = Requester.PostRequest(Methods.ChatHistory, jObject);
                var obj      = JsonConvert.DeserializeObject(response) as JObject;

                var chatHistory = new ChatHistory();

                var chatObject = obj["chats"] as JObject;
                foreach (var kvp in chatObject)
                {
                    foreach (JObject msgObject in kvp.Value as JArray)
                    {
                        var channel = string.Empty;
                        if (msgObject["channel"] != null)
                        {
                            channel = msgObject["channel"].ToString();
                        }

                        if (msgObject["is_join"] != null)
                        {
                            var presence = new Presence(
                                msgObject["id"].ToString(),
                                double.Parse(msgObject["t"].ToString()),
                                kvp.Key,
                                msgObject["from_user"].ToString(),
                                channel,
                                bool.Parse(msgObject["is_join"].ToString())
                                );
                            chatHistory.Push(presence);
                        }
                        else
                        {
                            var message = new Message(
                                msgObject["id"].ToString(),
                                double.Parse(msgObject["t"].ToString()),
                                kvp.Key,
                                msgObject["from_user"].ToString(),
                                channel,
                                msgObject["msg"].ToString()
                                );
                            chatHistory.Push(message);
                        }
                    }
                }
                LastPollTime = chatHistory.Last.Timestamp;
                ChatHistoryReceived?.Invoke(this, new ChatHistoryEventArgs(chatHistory));
            }
            catch (RequestException e)
            {
                ErrorOccured?.Invoke(this, new ErrorEventArgs(e));
            }
        }
Beispiel #3
0
 public void HandleChatHistoryReceived(object sender, ChatHistoryReceivedEventArgs e)
 {
     ChatHistoryReceived?.Invoke(this, e);
 }