示例#1
0
        void OnMessage(chat.Message msg)
        {
            #region
            if (msg.HasNotification)
            {
                if (msg.Notification.HasWelcome)
                {
                    //this.Title = msg.Notification.Welcome.Text.ToStringUtf8();
                }
                else if (msg.Notification.HasFriend)
                {
                    var name = msg.Notification.Friend.Name.ToStringUtf8();
                    if (msg.Notification.Friend.Online)
                    {
                        //my_friends.Add(name);
                    }
                    else
                    {
                        //my_friends.Remove(name);
                    }
                }
                else if (msg.Notification.HasScreen)
                {
                    if (OnGetScreen != null)
                        OnGetScreen(msg);
                }
                else if (msg.Notification.HasMsg)
                {
                    //var txtMsg = new TextMessage(msg.Notification.Msg);
                    //text_messages.Add(txtMsg);

                }
            }
            #endregion
        }
示例#2
0
        void OnMessage(chat.Message msg)
        {
            if (msg.HasNotification)
            {
                if (msg.Notification.HasWelcome)
                {
                    this.Title = msg.Notification.Welcome.Text.ToStringUtf8();
                }
                else if (msg.Notification.HasFriend)
                {
                    var name = msg.Notification.Friend.Name.ToStringUtf8();
                    if (msg.Notification.Friend.Online)
                    {
                        my_friends.Add(name);
                    }
                    else
                    {
                        my_friends.Remove(name);
                    }
                }
                else if (msg.Notification.HasMsg)
                {
                    var txtMsg = new TextMessage(msg.Notification.Msg);
                    text_messages.Add(txtMsg);

                    list_messages.SelectedIndex = list_messages.Items.Count - 1;
                    list_messages.ScrollIntoView(list_messages.SelectedItem);
                }
            }
        }
示例#3
0
        void OnReadBody(IAsyncResult ar)
        {
            try
            {
                int len = client.Client.EndReceive(ar);

                body_read_len += len;
                if (body_read_len < body.Length)
                {
                    client.Client.BeginReceive(body, body_read_len, body.Length - body_read_len, 0, new AsyncCallback(OnReadBody), null);
                    return;
                }
                StartReadHead();
            }
            catch (Exception ex)
            {
                Console.WriteLine("read body ..exception:{0}", ex.Message);
            }
            try
            {
                if (MessageHandler != null)
                {
                    chat.Message msg = chat.Message.ParseFrom(body);

                    if (msg.HasResponse)
                    {
                        Dispatcher.BeginInvoke((Action)(() =>
                        {
                            if (!msg.Response.Result)
                            {
                                errors.Add(msg.Response.ErrorDescribe.ToStringUtf8());
                            }
                            try
                            {
                                Transaction <chat.Message> .OnResponse(msg.Sequence, msg, msg.Response.LastResponse);
                            }
                            catch (Exception e)
                            {
                                errors.Add(e.Message);
                                errors.Add(e.StackTrace);
                            }
                        }));
                    }
                    else
                    {
                        //indication...
                        Dispatcher.BeginInvoke((Action)(() => { MessageHandler(msg); }));
                    }
                }
            }
            catch (Exception e)
            {
                Dispatcher.BeginInvoke((Action)(() =>
                {
                    errors.Add(e.Message);
                    errors.Add(e.StackTrace);
                }));
            }
        }
示例#4
0
        public void SendMessage(chat.Message msg)
        {
            int size = msg.SerializedSize;

            byte[] buf = new byte[size + 4];

            buf[0] = (byte)((size >> 24) & 0x000000ff);
            buf[1] = (byte)((size >> 16) & 0x000000ff);
            buf[2] = (byte)((size >> 8) & 0x000000ff);
            buf[3] = (byte)((size) & 0x000000ff);

            pb.CodedOutputStream cos = pb.CodedOutputStream.CreateInstance(buf, 4, size);
            msg.WriteTo(cos);

            SendMessage(buf);
        }