Exemplo n.º 1
0
        public MssgsClient(string mssgsApiHost, int mssgsApiPort, string name)
        {
            this.IsAuthenticated = false;
            this.IsConnected     = false;
            this.IsWelcomed      = false;
            this.factoryMethods  = new Dictionary <string, Func <RawMssgsResponse, MssgsResponse> >();
            this.Name            = name;
            this.OutgoingData    = new Queue <string>();
            this.Host            = mssgsApiHost;
            this.Port            = mssgsApiPort;

            this.RegisterResponseFactory <WelcomeMessage>("welcome", (RawMssgsResponse response) =>
            {
                response.Data.AssureHas("socketAPI");
                response.Data.AssureHas("webAPI");
                response.Data.AssureHas("credentials");
                return(new WelcomeMessage(response.Data["credentials"], response.Data["webAPI"], response.Data["socketAPI"]));
            }
                                                          );


            this.RegisterResponseFactory <MessagesList>("messages", (RawMssgsResponse rawResponse) =>
            {
                List <object> messagesraw = (List <object>)SimpleJson.SimpleJson.DeserializeObject(rawResponse.Data["0"]);
                var messages = new List <MssgsMessage>();

                foreach (object message in messagesraw)
                {
                    var rawMessage = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(message.ToString());
                    var msg        = MssgsMessage.Parse(
                        rawMessage.ToDictionary(k => k.Key.ToString(), k => k.Value != null ? k.Value.ToString() : ""),
                        false
                        );
                    messages.Add(msg);
                }
                return(new MessagesList(messages));
            }
                                                        );

            this.RegisterResponseFactory <MssgsMessage>("message", (RawMssgsResponse rawResponse) =>
            {
                return(MssgsMessage.Parse(rawResponse.Data, true));
            }
                                                        );

            this.ResponseReceived += (MssgsResponse response) =>
            {
                if (response.Method == "welcome")
                {
                    this.IsWelcomed = true;
                    if (this.Welcomed != null)
                    {
                        this.Welcomed((WelcomeMessage)response);
                    }
                }
                else if (response.Method == "messages")
                {
                    var list = (MessagesList)response;
                    if (this.MessageReceived != null)
                    {
                        foreach (var message in list.Messages)
                        {
                            if (this.InConversation)
                            {
                                this.Conversation.AddMessage(message);
                            }
                            this.MessageReceived(message);
                        }
                    }
                }
                if (response.Method == "message")
                {
                    var msg = (MssgsMessage)response;
                    if (this.InConversation)
                    {
                        this.Conversation.AddMessage(msg);
                    }
                    if (this.MessageReceived != null)
                    {
                        this.MessageReceived(msg);
                    }
                }
            };
        }
Exemplo n.º 2
0
 public void Send(MssgsMessage message)
 {
     this.ExecuteCommand(new SendMessageCommand(message));
 }