public static Visitor CreateNewVisitor(string accountId) { Visitor v = new Visitor(); VisitSession s = new VisitSession(); s.VisitingTime = DateTime.Now; s.VisitorId = v.VisitorId; v.AccountId = accountId; v.CurrentSession = s; s.Status = VisitSessionStatus.Visiting; // 浏览 return(v); }
public async Task <ActionResult <VisitSession> > GetVisitSessionOneLevelGrouping(Guid id) { var visit = await _context.Visit.Include(v => v.IpAddress) .FirstOrDefaultAsync(v => v.Id == id); if (visit == null) { return(NotFound()); } VisitSession visitSession = await _visitsService.GetVisitSessionByDeviceAndBrowserPair(visit); return(Ok(visitSession)); }
public void TestNewSession() { MyVisitSessionServiceProvider provider = new MyVisitSessionServiceProvider(); VisitSession v = new VisitSession(); VisitSessionService.Provider = provider; VisitSessionService.NewSession(v); Assert.AreEqual(1, provider.Sessions.Count); VisitSession vv = new VisitSession(); vv.SessionId = v.SessionId; VisitSessionService.NewSession(vv); Assert.AreEqual(1, provider.Sessions.Count); }
public void TestNewSessionMaxNum() { MyVisitSessionServiceProvider provider = new MyVisitSessionServiceProvider(); VisitSessionService.Provider = provider; for (int i = 0; i < 210; i++) { VisitSession s = new VisitSession(); s.Status = VisitSessionStatus.Leave; VisitSessionService.NewSession(s); } Assert.AreEqual(200, VisitSessionService.Sessions.Count); }
public static Visitor CreateNewVisitor(string accountId) { Visitor v = new Visitor(); VisitSession s = new VisitSession(); s.Location = "不知道"; s.VisitingTime = DateTime.Now; s.VisitorId = v.VisitorId; v.AccountId = accountId; v.CurrentSession = s; s.Status = VisitSessionStatus.Visiting; // 浏览 v.Name = "访客" + r.Next(1000); return(v); }
public void TestGetVisitorById() { VisitorService.Provider = new MyVisitorProvider(); Visitor v = new Visitor(); VisitSession s = new VisitSession(); s.VisitorId = v.VisitorId; s.Status = VisitSessionStatus.Leave; // 离开 v.AccountId = accountIdA; v.CurrentSession = s; VisitorService.NewVisitor(v); Assert.AreSame(v, VisitorService.GetVisitor(v.VisitorId)); Assert.IsNull(VisitorService.GetVisitor("dddd")); }
public void NewSession(VisitSession session) { string sql = string.Format("INSERT INTO LiveChat_VisitSession " + "([SessionId]" + " ,[VisitorId]" + " ,[IP]" + ",[Browser]" + " ,[Location]" + " ,[VisitingTime]" + " ,[DomainRequested]" + ",[OperatorId]" + " ,[Referrer]" + " ,[PageRequestCount]" + ",[Status])" + "VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", session.SessionId, session.VisitorId, session.IP, session.Browser, session.Location, session.VisitingTime, session.DomainRequested, session.OperatorId, session.Referrer, session.PageRequestCount, session.Status); DBHelper.ExecuteCommand(sql); }
/// <summary> /// 页面请求对话 /// </summary> /// <param name="CurrentVisitor"></param> /// <returns>ChatId</returns> public static string ChatPageRequestChat(Visitor visitor) { Trace.WriteLine(string.Format("ChatService.ChatPageRequestChat(Visitor = {0})", visitor)); if (!string.IsNullOrEmpty(visitor.Name)) { Visitor vs = VisitorService.GetVisitorById(visitor.VisitorId); vs.Name = visitor.Name; VisitorService.UpdateVisitor(vs); } Chat chatRequest = new Chat(); chatRequest.AccountId = visitor.AccountId; chatRequest.CreateTime = DateTime.Now; chatRequest.Status = ChatStatus.Requested; chatRequest.VisitorId = visitor.VisitorId; chats.Add(chatRequest); Provider.AddChat(chatRequest); VisitSession s = VisitSessionService.GetSessionById(visitor.CurrentSessionId); s.ChatRequestTime = DateTime.Now; s.Status = VisitSessionStatus.ChatRequesting; Message m = new Message(); m.ChatId = chatRequest.ChatId; m.Text = "正在接入客服,请稍等..."; m.Type = MessageType.SystemMessage_ToVisitor; m.Source = "系统"; m.Destination = "访客"; SendMessage(m, DateTime.Now.AddMilliseconds(100)); if (VisitorChatRequest != null) { VisitorChatRequestEventArgs req = new VisitorChatRequestEventArgs(visitor.VisitorId, chatRequest); visitorChatRequests.Add(req); VisitorChatRequest(null, req); } return(chatRequest.ChatId); }
public void TestGetSessionById() { MyVisitSessionServiceProvider provider = new MyVisitSessionServiceProvider(); VisitSessionService.Provider = provider; VisitSession v = new VisitSession(); v.SessionId = "aaaaa"; VisitSessionService.NewSession(v); Assert.AreEqual(v, VisitSessionService.GetSessionById(v.SessionId)); Assert.AreEqual(1, VisitSessionService.Sessions.Count); for (int i = 0; i < 20; i++) { VisitSession vv = new VisitSession(); vv.SessionId = "aaaaa"; VisitSessionService.NewSession(vv); } Assert.AreEqual(1, VisitSessionService.Sessions.Count); }
public void TestGetNewVisitor() { VisitorService.Provider = new MyVisitorProvider(); Visitor v = new Visitor(); VisitSession s = new VisitSession(); s.VisitorId = v.VisitorId; s.Status = VisitSessionStatus.Leave; // 离开 v.AccountId = accountIdA; v.CurrentSession = s; VisitorService.NewVisitor(v); DateTime time = DateTime.Now.AddDays(1); s.VisitingTime = time; VisitorService.NewVisitor(v); Assert.AreEqual(0, VisitorService.GetNewVisitors(accountIdA, time.Ticks).Count); Assert.AreEqual(1, VisitorService.GetNewVisitors(accountIdA, DateTime.Now.Ticks).Count); Assert.AreEqual(0, VisitorService.GetNewVisitors(accountIdA, DateTime.Now.AddDays(2).Ticks).Count); }
/// <summary> /// 接受客服邀请 /// </summary> /// <param name="chatId"></param> public static void AcceptOperatorInvitation(string chatId) { Trace.WriteLine(string.Format("ChatService.AcceptOperatorInvitation(ChatId = {0})", chatId)); Chat chat = GetChatById(chatId); if (chat != null) { chat.Status = ChatStatus.Accepted; if (chat.OperatorId != null) { OperatorService.GetOperatorById(chat.OperatorId).Status = OperatorStatus.Chatting; } VisitSession s = VisitSessionService.GetSessionById(VisitorService.GetVisitorById(chat.VisitorId).CurrentSessionId); s.Status = VisitSessionStatus.Chatting; //将访客状态改为对话中 s.ChatingTime = DateTime.Now; OperatorChatRequestEventArgs opq = new OperatorChatRequestEventArgs(chat.OperatorId, chat.VisitorId, chat); //tao OperatorChatRequestAccepted(null, new OperatorChatRequestAcceptedEventArgs(opq)); //tao } //SendMessage(new Message(chat.ChatId, "访客已接受对话邀请!", MessageType.SystemMessage_ToOperator)); SendMessage(new Message(chat.ChatId, "您已接受对话邀请!", MessageType.SystemMessage_ToVisitor)); }
public void TestNewVisitor() { VisitorService.Provider = new MyVisitorProvider(); Assert.AreEqual(0, VisitorService.GetAllOnlineVisitors(accountIdA).Count); Assert.AreEqual(0, VisitorService.GetAllOnlineVisitors(accountIdB).Count); Visitor v = new Visitor(); VisitSession s = new VisitSession(); s.VisitorId = v.VisitorId; s.Status = VisitSessionStatus.Leave; // 离开 v.AccountId = accountIdA; v.CurrentSession = s; VisitorService.NewVisitor(v); Assert.AreEqual(0, VisitorService.GetAllOnlineVisitors(accountIdA).Count); Assert.AreEqual(0, VisitorService.GetAllOnlineVisitors(accountIdB).Count); s.Status = VisitSessionStatus.Visiting; // 浏览 Assert.AreEqual(1, VisitorService.GetAllOnlineVisitors(accountIdA).Count); Assert.AreEqual(0, VisitorService.GetAllOnlineVisitors(accountIdB).Count); }
/// <summary> /// 跟据会话ID查询一条会话信息 /// </summary> /// <param name="sessionId"></param> /// <returns></returns> public VisitSession GetSessionById(string sessionId) { string sql = "select * from dbo.LiveChat_VisitSession where sessionid='" + sessionId + "'"; SqlDataReader data = null; VisitSession visitSession = null; try { using (data = DBHelper.GetReader(sql)) { if (data.Read()) { visitSession = new VisitSession(data); } return(visitSession); } } catch { throw; } }
/// <summary> /// 跟据访客会话取一行数据 /// </summary> /// <param name="sessionId"></param> /// <returns></returns> public static VisitSession GetSessionById(string sessionId) { Trace.WriteLine(string.Format("VisitSessionService.GetSessionById({0})", sessionId)); VisitSession vs = null; foreach (VisitSessionHit item in sessions) { if (item.Session.SessionId == sessionId) { vs = item.Session; break; } } if (vs == null) { vs = Provider.GetSessionById(sessionId); } if (vs == null) { Trace.WriteLine(string.Format("Error: VisitSessionService.GetSessionById({0}) error, can't find session in sessions or DB", sessionId)); } return(vs); }
public abstract void NewSession(VisitSession session); //保存一条新访客会话
public VisitorSessionChangeEventArgs(VisitSession visitSession) { this.VisitSession = visitSession; }
public VisitSessionHit(VisitSession s) { Session = s; LastHitTime = DateTime.Now; }
void processServerEvents(DataArriveEventArgs e) { // 客服状态改变 if (e.Data.GetType() == typeof(OperatorForceLogoffEventArgs)) { if (OperatorForceLogoff != null) { OperatorForceLogoff(this, null); } } else if (e.Data.GetType() == typeof(OperatorStatusChangeEventArgs)) { OperatorStatusChangeEventArgs os = (OperatorStatusChangeEventArgs)e.Data; Operator op = GetOperatorById(os.OperatorId); if (op != null) { op.Status = os.Status; OperatorStatusChanged(this, os); } } //访客对话请求 else if (e.Data.GetType() == typeof(VisitorChatRequestEventArgs)) { VisitorChatRequestEventArgs vc = (VisitorChatRequestEventArgs)e.Data; Visitor v = GetVisitorById(vc.VisitorId); if (v != null) { v.CurrentSession.Status = VisitSessionStatus.ChatRequesting; VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); VisitorChatRequest(this, vc); addChat(vc.Chat); } } //客服对话邀请 else if (e.Data.GetType() == typeof(OperatorChatRequestEventArgs)) { OperatorChatRequestEventArgs ocr = (OperatorChatRequestEventArgs)e.Data; Operator op = GetOperatorById(ocr.OperatorId); Visitor v = GetVisitorById(ocr.VisitorId); if (op != null && v != null) { op.Status = OperatorStatus.InviteChat; addChat(ocr.Chat); //v.CurrentSession.Status = VisitSessionStatus. //OperatorChatRequest(this, ocr); OperatorStatusChanged(this, new OperatorStatusChangeEventArgs(op.OperatorId, OperatorStatus.InviteChat)); } } // 访客对话请求被接受 else if (e.Data.GetType() == typeof(VisitorChatRequestAcceptedEventArgs)) { VisitorChatRequestAcceptedEventArgs v = e.Data as VisitorChatRequestAcceptedEventArgs; Chat chat = GetChatByChatId(v.VisitorChatRequest.Chat.ChatId); Operator op = GetOperatorById(v.VisitorChatRequest.Chat.OperatorId); Visitor visitor = GetVisitorById(v.VisitorChatRequest.VisitorId); if (chat != null && op != null && visitor != null) { chat.OperatorId = v.VisitorChatRequest.Chat.OperatorId; op.Status = OperatorStatus.Chatting; visitor.CurrentSession.OperatorId = op.OperatorId; VisitSession vs = visitor.CurrentSession; vs.ChatingTime = v.VisitorChatRequest.Chat.AcceptTime; vs.Status = VisitSessionStatus.Chatting; //VisitorChatRequestAccepted(this, (VisitorChatRequestAcceptedEventArgs)e.Data); OperatorStatusChanged(this, new OperatorStatusChangeEventArgs(v.VisitorChatRequest.Chat.OperatorId, OperatorStatus.Chatting)); VisitorSessionChange(this, new VisitorSessionChangeEventArgs(vs)); } } // 客服对话邀请被接受 else if (e.Data.GetType() == typeof(OperatorChatRequestAcceptedEventArgs)) { OperatorChatRequestAcceptedEventArgs ocr = (OperatorChatRequestAcceptedEventArgs)e.Data; Operator op = GetOperatorById(ocr.ChatRequest.OperatorId); Visitor v = GetVisitorById(ocr.ChatRequest.VisitorId); if (op != null && v != null) { op.Status = OperatorStatus.Chatting; v.CurrentSession.Status = VisitSessionStatus.Chatting; if (OperatorChatRequestAccepted != null) { OperatorChatRequestAccepted(this, (OperatorChatRequestAcceptedEventArgs)e.Data); } VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); OperatorStatusChanged(this, new OperatorStatusChangeEventArgs(op.OperatorId, op.Status)); } } // 客服对话邀请被拒绝 else if (e.Data.GetType() == typeof(OperatorChatRequestDeclinedEventArgs)) { OperatorChatRequestDeclinedEventArgs ocr = (OperatorChatRequestDeclinedEventArgs)e.Data; Operator op = GetOperatorById(ocr.ChatRequest.OperatorId); Visitor v = GetVisitorById(ocr.ChatRequest.VisitorId); Chat c = GetChatByChatId(ocr.ChatRequest.Chat.ChatId); if (op != null && v != null && c != null) { c.Status = ChatStatus.Decline; if (!IsOperatorHasActiveChat(op.OperatorId)) { op.Status = OperatorStatus.Idle; OperatorStatusChanged(this, new OperatorStatusChangeEventArgs(op.OperatorId, OperatorStatus.Idle)); } v.CurrentSession.Status = VisitSessionStatus.Visiting; VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); if (OperatorChatRequestDeclined != null) { OperatorChatRequestDeclined(this, (OperatorChatRequestDeclinedEventArgs)e.Data); } } } // 新的对话 else if (e.Data.GetType() == typeof(NewChatEventArgs)) { NewChat(this, (NewChatEventArgs)e.Data); } // 对话状态改变 else if (e.Data.GetType() == typeof(ChatStatusChangedEventArgs)) { ChatStatusChangedEventArgs cs = (ChatStatusChangedEventArgs)e.Data; Chat chat = GetChatByChatId(cs.ChatId); if (chat == null) { return; } Visitor v = GetVisitorById(chat.VisitorId); if (v == null) { return; } chat.Status = cs.Status; Operator o = GetOperatorById(chat.OperatorId); if (chat.Status == ChatStatus.Accepted) { v.CurrentSession.Status = VisitSessionStatus.Chatting; VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); o.Status = OperatorStatus.Chatting; OperatorStatusChanged(this, new OperatorStatusChangeEventArgs(chat.OperatorId, OperatorStatus.Chatting)); } else if (chat.Status == ChatStatus.Closed) { v.CurrentSession.Status = VisitSessionStatus.Visiting; VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); if (o != null && !IsOperatorHasActiveChat(chat.OperatorId)) { o.Status = OperatorStatus.Idle; OperatorStatusChanged(this, new OperatorStatusChangeEventArgs(chat.OperatorId, OperatorStatus.Idle)); } } if (ChatStatusChanged != null) { ChatStatusChanged(this, cs); } } else if (e.Data.GetType() == typeof(OperatorChatJoinInviteEventArgs)) { ChatJoinInvite(this, (OperatorChatJoinInviteEventArgs)e.Data); } else if (e.Data.GetType() == typeof(OperatorChatJoinInviteAcceptedEventArgs)) { ChatJoinInviteAccepted(this, (OperatorChatJoinInviteAcceptedEventArgs)e.Data); } else if (e.Data.GetType() == typeof(OperatorChatJoinInviteDeclinedEventArgs)) { ChatJoinInviteDeclined(this, (OperatorChatJoinInviteDeclinedEventArgs)e.Data); } // 新消息 else if (e.Data.GetType() == typeof(ChatMessageEventArgs) && NewMessage != null) { NewMessage(this, (ChatMessageEventArgs)e.Data); } // 新访问,访客可能已经存在 else if (e.Data.GetType() == typeof(NewVisitingEventArgs) && NewVisiting != null) { NewVisitingEventArgs nv = (NewVisitingEventArgs)e.Data; nv.Visitor.CurrentSession = nv.Session; Visitor v = GetVisitorById(nv.Visitor.VisitorId); if (v != null) { v.CurrentSession = nv.Session; VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); } else { addVisitor(nv.Visitor); NewVisiting(this, (NewVisitingEventArgs)e.Data); } } // 访客离开 else if (e.Data.GetType() == typeof(VisitorLeaveEventArgs)) { VisitorLeaveEventArgs vl = e.Data as VisitorLeaveEventArgs; Visitor v = GetVisitorById(vl.VisitorId); if (v != null) { v.CurrentSession.Status = VisitSessionStatus.Leave; VisitorSessionChange(this, new VisitorSessionChangeEventArgs(v.CurrentSession)); } //VisitorLeave(this, (VisitorLeaveEventArgs)e.Data); } }
public NewVisitingEventArgs(Visitor visitor, VisitSession session) { this.Visitor = visitor; this.Session = session; }