Example #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;
            }
        }
Example #2
0
 public void HandleFilteredMessagesReceived(object sender, FilteredMessagesReceivedEventArgs e)
 {
     FilteredMessagesReceived?.Invoke(this, e);
 }