Ejemplo n.º 1
0
        public void Send(string receiveMessage, string bgNo)
        {
            var log = new Log("Chat/sendOne/" + bgNo + "");

            log.Write(receiveMessage);
            log.Write("uesr:" + JsonConvert.SerializeObject(UserList) + "");
            var    messageInfo    = JsonConvert.DeserializeObject <ReceiveMessage>(receiveMessage);
            var    returnMessage  = new ReturnMessage();
            string toConnectionId = "";
            string clientName     = "";
            string clientId       = Context.GetHttpContext().Request.Query["clientId"].ToString();
            var    user           = UserList.Where(c => c.ConnectionId == Context.ConnectionId).FirstOrDefault();

            if (user != null)
            {
                clientName = user.UserNickName;
                //messageInfo.message = HttpUtility.HtmlEncode(messageInfo.message).Replace("\r\n", "<br/>").Replace("\n", "<br/>");
                var toUser = UserList.Where(c => c.UserId == messageInfo.toUserId).FirstOrDefault();
                if (toUser != null)
                {
                    returnMessage.userId      = messageInfo.userId;
                    returnMessage.userName    = clientName;
                    returnMessage.userUrl     = user.Url;
                    returnMessage.message     = messageInfo.message;
                    returnMessage.messageDate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    returnMessage.toUserId    = messageInfo.toUserId;
                    var json = JsonConvert.SerializeObject(returnMessage);
                    toConnectionId = toUser.ConnectionId;
                    Clients.Client(toConnectionId).SendAsync("receiveMessage", json);
                }
                var ManageUser = UserList.Where(c => c.UserType != 0 && c.BgNo == bgNo).ToList();
                if (ManageUser.Count > 0)
                {
                    returnMessage.userId      = messageInfo.userId;
                    returnMessage.userName    = clientName;
                    returnMessage.userUrl     = user.Url;
                    returnMessage.message     = messageInfo.message;
                    returnMessage.messageDate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    returnMessage.toUserId    = messageInfo.toUserId;
                    var json = JsonConvert.SerializeObject(returnMessage);
                    var list = ManageUser.Select(c => c.ConnectionId).ToList();
                    log.Write("connects:" + JsonConvert.SerializeObject(list) + "");
                    Clients.Clients(list).SendAsync("receiveMessage", json);
                }
                else
                {
                    var clientInfo = UserList.Where(c => c.UserId == clientId).FirstOrDefault();
                    if (_chatHelper.IsToIris(clientInfo.BgNo))
                    {
                        var openId = "o5QuWws4RNxB7rZCYPzh0Qyw89AU";
                        if (messageInfo.toUserId.Length != openId.Length)
                        {
                            _chatHelper.ChatMessageToIris(messageInfo.toUserId, messageInfo.toUserName, clientId, clientName, messageInfo.message, bgNo, log);
                        }
                    }
                }
                _chatHelper.AddChatMessage(messageInfo, log);
                if (messageInfo.userId.IndexOf("o") > -1)
                {
                    _chatHelper.AddBehavviorInfo(messageInfo, log);
                }
            }
        }