Beispiel #1
0
        private void show_msg(UserMessage userMessage)
        {
            Dictionary <String, String> src = new Dictionary <string, string>()
            {
                { "", "" }
            };
            Dictionary <String, String> dst   = null;
            Dictionary <String, String> group = null;
            JObject msg = userMessage.getRawMsg();

            String content = ((JValue)msg["Content"]).Value.ToString();

            content = content.Replace("&lt;", "<").Replace("&gt;", ">");

            String msg_id = ((JValue)msg["MsgId"]).Value.ToString();

            // 接收到来自群的消息
            if (((JValue)msg["FromUserName"]).Value.ToString().Substring(0, 2) == "@@")
            {
                String groupId = ((JValue)msg["FromUserName"]).Value.ToString();
                group = this.getGroupById(groupId);
                if (content.Contains(":<br/>"))
                {
                    String u_id = content.Split(new string[] { ":<br/>" }, StringSplitOptions.None)[0];
                    src = this.getGroupUserById(u_id, groupId);
                    dst = new Dictionary <string, string>()
                    {
                        { "ShowName", "GROUP" }
                    };
                }
                else
                {
                    String u_id = ((JValue)msg["ToUserName"]).Value.ToString();
                    src = new Dictionary <string, string>()
                    {
                        { "ShowName", "SYSTEM" }
                    };
                    dst = getGroupUserById(u_id, groupId);
                }
            }
            else
            {
                // 非群聊消息
                src = this.getUserById(((JValue)msg["FromUserName"]).Value.ToString());
                dst = this.getUserById(((JValue)msg["ToUserName"]).Value.ToString());
            }


            if (null != group)
            {
                WriteLog("{0} |{1}| {2} -> {3}: {4}\n", msg_id, group["ShowName"], src["ShowName"],
                         dst["ShowName"], userMessage.getLog());
            }
            else
            {
                string newMsg = string.Format(DateTime.Now.ToString(" hh:mm:ss ") + "{0} {1} -> {2}: {3}\n", msg_id, src["ShowName"],
                                              dst["ShowName"], userMessage.getLog());

                WriteLog(newMsg);
            }
            //显示到界面上
            string showMsg = DateTime.Now.ToString(" hh:mm:ss ") + src["ShowName"] + ":" + userMessage.getLog() + "\r\n";

            if (FormLogin.formMain.isCurr(src["UserName"])) //发来消息是否来自于当前聊天窗口用户
            {
                FormLogin.formMain.AddtextBoxHistory(showMsg);
            }
            else
            {
                FormLogin.formMain.SetTitle(showMsg);
            }
            listUserMsg.Add(userMessage);
        }
Beispiel #2
0
        /// <summary>
        /// 处理发来的消息
        /// </summary>
        /// <param name="dic"></param>
        public void handle_msg(JObject dic)
        {
            WriteLog("handle message");
            if (null != messageHandle)
            {
                messageHandle.WxSync(dic);
            }

            int intMegCount = dic["AddMsgList"].Count();

            if (intMegCount == 0)
            {
                return;
            }

            WriteLog(Const.LOG_MSG_NEW_MSG, intMegCount);

            JArray msgs = (JArray)dic["AddMsgList"];

            foreach (JObject element in msgs)
            {
                //JsonObject msg = element.getAsJsonObject();
                String      msgType     = ((JValue)element["MsgType"]).Value.ToString();
                String      msgId       = ((JValue)element["MsgId"]).Value.ToString();
                String      content     = ((JValue)element["Content"]).Value.ToString().Replace("&lt;", "<").Replace("&gt;", ">");
                UserMessage userMessage = new UserMessage(this);
                userMessage.setRawMsg(element);

                switch ((EnumMsgType)(int.Parse(msgType)))
                {
                case EnumMsgType.MSGTYPE_TEXT:
                    // 地理位置消息
                    if (content.Contains("pictype=location"))
                    {
                        String location = content.Split(new string[] { "<br/>" }, StringSplitOptions.None)[1];
                        userMessage.setLocation(location);
                        userMessage.setLog(String.Format(Const.LOG_MSG_LOCATION, location));
                    }
                    else
                    {
                        // 普通文本
                        String text = null;
                        if (content.Contains(":<br/>"))
                        {
                            text = content.Split(new string[] { ":<br/>" }, StringSplitOptions.None)[1];
                        }
                        else
                        {
                            text = content;
                        }
                        userMessage.setText(text);
                        userMessage.setLog(text.Replace("<br/>", "\n"));
                    }
                    break;

                case EnumMsgType.MSGTYPE_IMAGE:
                    WriteLog(Const.LOG_MSG_PICTURE, "");
                    break;

                case EnumMsgType.MSGTYPE_STATUSNOTIFY:
                    WriteLog(Const.LOG_MSG_NOTIFY_PHONE);
                    break;
                }
                //// 文本groupMessage
                //if (conf["MSGTYPE_TEXT"] == msgType)
                //{

                //}
                //else if (conf["MSGTYPE_STATUSNOTIFY"] == msgType)
                //{
                //    WriteLog(Const.LOG_MSG_NOTIFY_PHONE);
                //    return;
                //}

                this.show_msg(userMessage);

                Boolean isGroupMsg = (((JValue)element["FromUserName"]).Value.ToString() + ((JValue)element["ToUserName"]).Value.ToString()).Contains("@@");
                if (isGroupMsg)
                {
                    GroupMessage groupMessage = make_group_msg(userMessage);
                    if (null != messageHandle)
                    {
                        messageHandle.groupMessage(groupMessage);
                    }
                }
                else
                {
                    if (null != messageHandle)
                    {
                        messageHandle.userMessage(userMessage);
                    }
                }
            }
        }