Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
protected void Page_Load(object sender, EventArgs e)
{
 
if (Request.QueryString["action"] != null)
{
if (Session["chatHistory"] == null)
{
Session["chatHistory"] = new Hashtable(); ;
}
if (Session["openChatBoxes"] == null)
{
Session["openChatBoxes"] = new Hashtable();
}
if (Session["tsChatBoxes"] == null)
{
Session["tsChatBoxes"] = new Hashtable();
}
switch (Request.QueryString["action"].ToString())
{
case "chatheartbeat":
{
chatHeartbeat();
break;
}
case "sendchat":
{
sendChat();
break;
}
case "closechat":
{
closeChat();
break;
}
case "startchatsession":
{
startChatSession();
break;
}
}
}
 
Hashtable s = new Hashtable();
}
private void sendChat()
{
string from = Request.Form["from"];
Session["username"] = from; // Assign Session user name value to $from value
string to = Request.Form["to"]; // Assign POST FORM 'To' value to $to value}
string message = Request.Form["message"]; // Assign POST FORM 'message' value to $message value
 

if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[to] = DateTime.Now.ToString("Y-m-d H:i:s");
 
string messagesan = sanitize(message);
if (Session["chatHistory"] != null)
{
if (((Hashtable)(Session["chatHistory"])).Contains(Request.Form["to"]))
{
((Hashtable)(Session["chatHistory"]))[to] = "";
//((Hashtable)(Session["chatHistory"]))[getIndexOf(((Hashtable)(Session["chatHistory"])), Request["to"])] = "";
}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[to] += "\"s\":\"1\",\"f\":\"{" + to + "}\",\"m\":\"{" + messagesan + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[to] = null;
 
/* Add Insert Query here*/
SqlDataProvider sqd4 = new SqlDataProvider();
string ConnectionString4 = sqd4.ConnectionString;
SqlConnection cnn4 = new SqlConnection(ConnectionString4);
cnn4.Open();
string query4 = "insert into dnn_DNAiusMessage_1 (UserFrom,UserTo,MessageContent,SendTime,Received) Values('" + from + "','" + to + "','" + message
+ "','" + DateTime.Now + "','" + 0 + "')";
SqlCommand cmd4 = new SqlCommand(query4, cnn4);
SqlDataReader reader = cmd4.ExecuteReader();
Response.Write("1");
Response.End();
}
private string sanitize(string text)
{
//text = htmlspecialchars(text, ENT_QUOTES); // Convert the html special characters
text = text.Replace("\n\r", "\n");
text = text.Replace("\r\n", "\n");
text = text.Replace("\n", "
");
return text;
}
private void startChatSession()
{
string from = Request.Form["from"];
Session["username"] = from;
string items = "";
if (Session["openChatBoxes"] != null)
{ // Check the Session variable
System.Collections.Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{ // Loop the session array
items += chatBoxSession(i); // Append to Items variable
}
}
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
HttpContext.Current.Response.Write("\"username\": \"" + HttpContext.Current.User.Identity.Name + "\","); // Display the Username Which is set in the session
HttpContext.Current.Response.Write("\"items\": [");
HttpContext.Current.Response.Write(items);
HttpContext.Current.Response.Write("]");
HttpContext.Current.Response.Write("}");
Response.End();
}
private string chatBoxSession(int chatbox)
{
 
string items = "";
 
if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{ // Check the Session variable
items = ((Hashtable)(Session["chatHistory"]))[chatbox].ToString();
}
 
return items;
}
private void closeChat()
{
int chatbox = 1; //value of chatbox
try
{
((Hashtable)(Session["openChatBoxes"]))[chatbox] = null;
}
catch
{
}
Response.Write("1");
}
private int getIndexOf(Hashtable SessionArray, string Value)
{
// try
// {
// return SessionArray.in((object)Value);
// }
// catch
// {
return 0;
//}
}
 
private void chatHeartbeat()
{
//string to = Request.QueryString["UserName"].ToString();
// Session["username1"] = to; // Assign Session user name value to $from value
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */
string ConString = ConfigurationManager.AppSettings["SiteSqlServer"];
CHATDataContext ObjDataContext = new CHATDataContext(ConString);
// var messages = ObjDataContext.GetNotRecieved(User.Identity.Name);
var q = from a in ObjDataContext.dnn_DNAiusMessage_1
where a.UserFrom == User.Identity.Name && a.Received == Convert.ToInt32(1)
select a;
 

// System.Collections.Generic.GenericCollection messages = ObjDataContext.GetNotRecieved(User.Identity.Name);
// System.Collections.Generic.List messages = ObjDataContext.GetNotRecieved(User.Identity.Name);
List messages = q.AsEnumerable().ToList();
string items = "";
 
foreach (dnn_DNAiusMessage_1 message in q)
{
if (Session["openChatBoxes"] != null && Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["openChatBoxes"])).Contains(message.UserFrom) && ((Hashtable)(Session["chatHistory"])).Contains(message.UserFrom))
{
items = ((Hashtable)(Session["chatHistory"]))[message.UserFrom].ToString();
}
}
 
message.MessageContent = sanitize(message.MessageContent);
// Response.Write(message.MessageContent);
//items = items + "{\"s\": \"0\",\"f\": \"" + message.UserFrom.ToString() + "\",\"m\": \"" + "ggg" + "\"}, ";
//items = items + "\"s\":\"0\",\"f\":\"{" + message.UserFrom.ToString() + "}\",\"m\":\"{" + "GOOOOP" + "}\"";
 
items = items + "{\"s\": \"0\",\"f\": \"" + message.UserFrom.ToString() + "\",\"m\": \"" + message.MessageContent + "\"}, ";
 
if (Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["chatHistory"])).Contains(message.UserFrom))
{
((Hashtable)(Session["chatHistory"]))[message.UserFrom] = "";
 
}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[message.UserFrom.ToString()] += "\"s\":\"0\",\"f\":\"{" + message.UserFrom.ToString() + "}\",\"m\":\"{" + message.MessageContent + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[message.UserFrom.ToString()] = null; // Unset means clear the session value
if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[message.UserFrom.ToString()] = message.SendTime;
 

}
if (Session["openChatBoxes"] != null)
{
Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{
int chatbox = i;
if (openChatBoxesArray[i] != null)
{
DateTime time = (DateTime)(openChatBoxesArray[i]);
string timeval = "";
TimeSpan Now = (TimeSpan)(DateTime.Now - time);
timeval = time.ToString("g:iA M dS");
string message1 = "Sent at " + timeval;
 
if (Now.Minutes > 180)
{
// items = items + "{\"s\": \"0\",\"f\": \"" + message.UserFrom.ToString() + "\",\"m\": \"" + message.MessageContent + "\"}, ";
items += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message1 + "}\"";
if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{
((Hashtable)(Session["chatHistory"]))[chatbox] = "";
}
((Hashtable)(Session["chatHistory"]))[chatbox] += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message1 + "}\"";
((Hashtable)(Session["tsChatBoxes"]))[chatbox] = "0";
}
}
}
}
ObjDataContext.UpdateRecieved(User.Identity.Name);
ObjDataContext.SubmitChanges();
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
 
}
HttpContext.Current.Response.Write("{ ");
Response.Write("\"items\":[" + items + "]");
HttpContext.Current.Response.Write(" }");
Response.End();
 
}

Chat Application Code is not Working .Please any
one help me. problem is i am not recieving the message
it will be stored into database.but i am not getting the message in browser. please help me
Posted
Updated 11-Dec-12 17:01pm
v2
Comments
GopinathGuru 12-Dec-12 1:26am    
Hello Rohit,
You have solution for above one ?
ravuravu 16-Mar-13 2:48am    
can u pls share the chatdatacontext class file coding

1 solution

 
Share this answer
 
Comments
GopinathGuru 12-Dec-12 0:24am    
But in my web application no need for chat room. I directly fetch the online user from db and display into online Chat list. Now i need to provide the chat funtionality between online user.Thanks for providing the above solution.
Suvabrata Roy 12-Dec-12 0:25am    
Just modify and enjoy coding :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900