1 using System;

    2 using System.Collections;

    3 using System.ComponentModel;

    4 using System.Data;

    5 using System.Linq;

    6 using System.Web;

    7 using System.Web.Services;

    8 using System.Web.Services.Protocols;

    9 using System.Xml.Linq;

   10 using System.Collections.Generic;

   11 using WEBSWAPP.Website.Data;

   12 

   13 namespace Webswapp.Website.Categories

   14     {

   15     /// <summary>

   16     /// Summary description for Chat

   17     /// </summary>

   18     [WebService(Namespace = "http://webswapp.com/")]

   19     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   20     [ToolboxItem(false)]

   21     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

   22     [System.Web.Script.Services.ScriptService]

   23     public class Chat : System.Web.Services.WebService

   24         {

   25 

   26         [WebMethod(EnableSession = true)]

   27         public List<ChatUser> UsersList()

   28             {

   29             return HttpContext.Current.Session["ChatUsersList"] as List<ChatUser>;

   30             }

   31 

   32         [WebMethod(EnableSession = true)]

   33         public List<ChatEntry> SendMessageToCustomerSupport(string userName, string message)

   34             {

   35             SortedList<string, ChatEntry> chatList = null;

   36 

   37                 List<ChatUser> users = HttpContext.Current.Session["ChatUsersList"] as List<ChatUser>;

   38             //notice that you will be talking to yourself in this demo because I only store users on

   39             //the session level for the purpose of this demo but for a real production application

   40             //I would propabably use a data store or the application cache

   41                 if (users == null) users = new List<ChatUser>();

   42                 var user = users.Where(u => u.UserName.Equals(userName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault ();

   43                 if (user == null)

   44                     users.Add(new ChatUser() { UserName = userName, TimeOfStartingSession = DateTime.Now.ToUniversalTime().ToString() });

   45 

   46             //store the users in the Session object

   47                 HttpContext.Current.Session["ChatUsersList"] = users;

   48 

   49             //Store the user's message in a list for that user

   50                 chatList = HttpContext.Current.Session[userName + "_ChatEntries"] as SortedList<string , ChatEntry>;

   51                 if (chatList == null)    //if this is the first message by that user then initialize the list

   52                     {

   53                     chatList = new SortedList<string, ChatEntry>();

   54                     }

   55             chatList.Add (DateTime.Now.ToUniversalTime().ToString(),  new ChatEntry()

   56                 {

   57                     EntryDate = DateTime.Now.ToUniversalTime().ToString(),

   58                     Message = message,

   59                     UserName = userName

   60                 });

   61                 HttpContext.Current.Session[userName + "_ChatEntries"] = chatList;

   62                 return chatList.Select (c=>c.Value ).ToList();

   63             }

   64 

   65         [WebMethod(EnableSession = true)]

   66         public List<ChatEntry> GetDiscussionByUserName(string userName)

   67             {

   68             List<ChatEntry> ret = null;

   69             SortedList<string, ChatEntry> chatList = HttpContext.Current.Session[userName + "_ChatEntries"] as SortedList<string, ChatEntry>;

   70             if (chatList != null)

   71                 {

   72                 ret =chatList.Select(c => c.Value).ToList();

   73                 }

   74             return ret;

   75             }

   76 

   77         [WebMethod(EnableSession = true)]

   78         public List<ChatEntry> PostResponseToUser(string userName, string messageEntryDate, string response)

   79             {

   80             SortedList<string , ChatEntry> chatList = HttpContext.Current.Session[userName + "_ChatEntries"] as SortedList<string, ChatEntry>;

   81             var chatEntry = chatList[messageEntryDate];

   82             if (chatEntry != null)

   83                 {

   84                 chatEntry.Response = response;

   85                 chatEntry.ResponseDate = DateTime.Now.ToUniversalTime().ToString();

   86                 }

   87 

   88             return chatList.Select(c => c.Value).ToList();

   89             }

   90         }

   91     }