Ejemplo n.º 1
0
        private async void Client_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            using (DataReader reader = args.GetDataReader())
                try
                {
                    string str = reader.ReadString(reader.UnconsumedBufferLength).Trim('\0');
                    if (!string.IsNullOrEmpty(str))
                    {
                        JObject json = JObject.Parse(str);
                        switch (json["event"].ToString())
                        {
                        case "auth":
                            if ((bool)json["needsAuth"])
                            {
                                string login = string.Format("{{\"username\":\"{0}\",\"password\":\"{1}\"}}", AuthenticationCredentials.UserName.Replace("\"", "\\\""), AuthenticationCredentials.Password.Replace("\"", "\\\""));
                                clientWriter.WriteString(login);
                                await clientWriter.FlushAsync();
                            }
                            break;

                        case "update":
                            EventType  eventType  = (EventType)Enum.Parse(typeof(EventType), json["eventType"].ToString());
                            ObjectType objectType = (ObjectType)Enum.Parse(typeof(ObjectType), json["objectType"].ToString());
                            IIdObject  obj        = JsonToObject(objectType, (JObject)json["obj"]);
                            switch (eventType)
                            {
                            case EventType.Add:
                                switch (objectType)
                                {
                                case ObjectType.Recipient:
                                    recipients.Add(obj.Id, obj as Recipient);
                                    break;

                                case ObjectType.Conversation:
                                    conversations.Add(obj.Id, obj as Conversation);
                                    break;

                                case ObjectType.Message:
                                    messages.Add(obj.Id, obj as Message);
                                    break;

                                case ObjectType.Attachment:
                                    attachments.Add(obj.Id, obj as Attachment);
                                    break;
                                }
                                break;

                            case EventType.Update:
                                switch (objectType)
                                {
                                case ObjectType.Recipient:
                                    UpdateObject(recipients[obj.Id], obj);
                                    break;

                                case ObjectType.Conversation:
                                    UpdateObject(conversations[obj.Id], obj);
                                    break;

                                case ObjectType.Message:
                                    UpdateObject(messages[obj.Id], obj);
                                    break;

                                case ObjectType.Attachment:
                                    UpdateObject(attachments[obj.Id], obj);
                                    break;
                                }
                                break;

                            case EventType.Remove:
                                switch (objectType)
                                {
                                case ObjectType.Recipient:
                                    recipients.Remove(obj.Id);
                                    break;

                                case ObjectType.Conversation:
                                    conversations.Remove(obj.Id);
                                    break;

                                case ObjectType.Message:
                                    messages.Remove(obj.Id);
                                    break;

                                case ObjectType.Attachment:
                                    attachments.Remove(obj.Id);
                                    break;
                                }
                                break;
                            }
                            StreamUpdate?.Invoke(this, new StreamUpdateEventArgs()
                            {
                                ObjectType = objectType,
                                EventType  = eventType,
                                Object     = obj
                            });
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    StopStream();
                    StreamUpdate?.Invoke(this, new StreamUpdateEventArgs()
                    {
                        Error = ex
                    });
                }
        }
Ejemplo n.º 2
0
        async void StreamClientThread()
        {
            try
            {
                while (running)
                {
                    var buffer  = new byte[1024];
                    var segment = new ArraySegment <byte>(buffer);
                    var result  = await client.ReceiveAsync(segment, cancelToken.Token);

                    string str = Encoding.UTF8.GetString(buffer).Trim('\0');
                    if (!string.IsNullOrEmpty(str))
                    {
                        JObject json = JObject.Parse(str);
                        switch (json["event"].ToString())
                        {
                        case "auth":
                            if ((bool)json["needsAuth"])
                            {
                                string login = string.Format("{{\"username\":\"{0}\",\"password\":\"{1}\"}}", AuthenticationCredentials.UserName.Replace("\"", "\\\""), AuthenticationCredentials.Password.Replace("\"", "\\\""));
                                await client.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(login)), WebSocketMessageType.Text, true, cancelToken.Token);
                            }
                            break;

                        case "update":
                            EventType  eventType  = (EventType)Enum.Parse(typeof(EventType), json["eventType"].ToString());
                            ObjectType objectType = (ObjectType)Enum.Parse(typeof(ObjectType), json["objectType"].ToString());
                            IIdObject  obj        = JsonToObject(objectType, (JObject)json["obj"]);
                            switch (eventType)
                            {
                            case EventType.Add:
                                switch (objectType)
                                {
                                case ObjectType.Recipient:
                                    recipients.Add(obj.Id, obj as Recipient);
                                    break;

                                case ObjectType.Conversation:
                                    conversations.Add(obj.Id, obj as Conversation);
                                    break;

                                case ObjectType.Message:
                                    messages.Add(obj.Id, obj as Message);
                                    break;

                                case ObjectType.Attachment:
                                    attachments.Add(obj.Id, obj as Attachment);
                                    break;
                                }
                                break;

                            case EventType.Update:
                                switch (objectType)
                                {
                                case ObjectType.Recipient:
                                    UpdateObject(recipients[obj.Id], obj);
                                    break;

                                case ObjectType.Conversation:
                                    UpdateObject(conversations[obj.Id], obj);
                                    break;

                                case ObjectType.Message:
                                    UpdateObject(messages[obj.Id], obj);
                                    break;

                                case ObjectType.Attachment:
                                    UpdateObject(attachments[obj.Id], obj);
                                    break;
                                }
                                break;

                            case EventType.Remove:
                                switch (objectType)
                                {
                                case ObjectType.Recipient:
                                    recipients.Remove(obj.Id);
                                    break;

                                case ObjectType.Conversation:
                                    conversations.Remove(obj.Id);
                                    break;

                                case ObjectType.Message:
                                    messages.Remove(obj.Id);
                                    break;

                                case ObjectType.Attachment:
                                    attachments.Remove(obj.Id);
                                    break;
                                }
                                break;
                            }
                            StreamUpdate?.Invoke(this, new StreamUpdateEventArgs()
                            {
                                ObjectType = objectType,
                                EventType  = eventType,
                                Object     = obj
                            });
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                StopStream();
                StreamUpdate?.Invoke(this, new StreamUpdateEventArgs()
                {
                    Error = ex
                });
            }
        }