this php code from
http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/[
^]
The convert php to c#/vb not too tough but here problem will not solve from this conversion. here added some js & jquery file with php file.
yet...Keep Try!!
The code as after conversion ..(its not my code)
C# code..........................
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
namespace wwwroot.chat
{
public partial class chathandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["action"] != null)
{
switch (Request.QueryString["action"].ToString())
{
case "chatheartbeat":
{
chatHeartbeat();
break;
}
case "sendchat":
{
sendChat();
break;
}
case "closechat":
{
closeChat();
break;
}
case "startchatsession":
{
startChatSession();
break;
}
}
}
if (Session["chatHistory"] == null)
{
Session["chatHistory"] = new ArrayList(); ;
}
if (Session["openChatBoxes"] == null)
{
Session["openChatBoxes"] = new ArrayList();
}
if(Session["tsChatBoxes"]==null)
{
Session["tsChatBoxes"] = new ArrayList();
}
ArrayList s = new ArrayList();
}
private void chatHeartbeat()
{
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */
GenericCollection<Chat> messages = Chat.GetNotRecieved(User.Identity.Name);
string items = "";
foreach (Chat message in messages)
{
if (!((ArrayList)(Session["openChatBoxes"])).Contains(message.UsernameFrom) && ((ArrayList)(Session["chatHistory"])).Contains(message.UsernameFrom))
{
items = ((ArrayList)(Session["chatHistory"]))[((ArrayList)(Session["chatHistory"])).IndexOf((object)(message.UsernameFrom))].ToString();
}
message.Message = sanitize(message.Message);
items = items + "\"s\":\"0\",\"f\":\"{" + message.UsernameFrom.ToString() + "}\",\"m\":\"{" + message.Message + "}\"";
if (!((ArrayList)(Session["chatHistory"])).Contains(message.UsernameFrom))
{
((ArrayList)(Session["chatHistory"]))[((ArrayList)(Session["chatHistory"])).IndexOf((object)(message.UsernameFrom))] = "";
}
((ArrayList)(Session["chatHistory"]))[getIndexOf(((ArrayList)(Session["chatHistory"])), message.UsernameFrom.ToString())] += "\"s\":\"0\",\"f\":\"{" + message.UsernameFrom.ToString() + "}\",\"m\":\"{" + message.Message + "}\"";
((ArrayList)(Session["tsChatBoxes"]))[getIndexOf(((ArrayList)(Session["tsChatBoxes"])), message.UsernameFrom.ToString())] = null; // Unset means clear the session value
((ArrayList)(Session["openChatBoxes"]))[getIndexOf(((ArrayList)(Session["openChatBoxes"])), message.UsernameFrom.ToString())] = message.Sent;
}
if (Session["openChatBoxes"] != null)
{
ArrayList openChatBoxesArray = (ArrayList)(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 message = "Sent at " + timeval;
if (Now.Minutes > 180)
{
items += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
if (((ArrayList)(Session["chatHistory"]))[chatbox] != null)
{
((ArrayList)(Session["chatHistory"]))[chatbox] = "";
}
((ArrayList)(Session["chatHistory"]))[chatbox] += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
((ArrayList)(Session["tsChatBoxes"]))[chatbox] = "1";
}
}
}
}
Chat.UpdateRecieved(User.Identity.Name);
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();
}
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", "<br>");
return text;
}
private int getIndexOf(ArrayList SessionArray, string Value)
{
try
{
return SessionArray.IndexOf((object) Value);
}
catch
{
return 0;
}
}
private string chatBoxSession(int chatbox)
{
string items = "";
if (((ArrayList)(Session["chatHistory"]))[chatbox] != null)
{ // Check the Session variable
items = ((ArrayList)(Session["chatHistory"]))[chatbox].ToString();
}
return items;
}
private void sendChat()
{
string from = User.Identity.Name; // Assign Seesion user name value to $from value
string to = Request["to"]; // Assign POST FORM 'To' value to $to value}
string message = Request["message"]; // Assign POST FORM 'message' value to $message value
((ArrayList)(Session["openChatBoxes"]))[getIndexOf(((ArrayList)(Session["openChatBoxes"])), Request["to"])] = DateTime.Now.ToString("Y-m-d H:i:s");
string messagesan = sanitize(message);
if (((ArrayList)(Session["chatHistory"])).Contains(Request["to"]))
{
((ArrayList)(Session["chatHistory"]))[getIndexOf(((ArrayList)(Session["chatHistory"])), Request["to"])] = "";
}
((ArrayList)(Session["chatHistory"]))[getIndexOf(((ArrayList)(Session["chatHistory"])), Request["to"])] += "\"s\":\"1\",\"f\":\"{" + to + "}\",\"m\":\"{" + messagesan + "}\"";
((ArrayList)(Session["tsChatBoxes"]))[getIndexOf(((ArrayList)(Session["tsChatBoxes"])), Request["to"])] = null;
/* Add Insert Query here*/
Chat chatObj = new Chat();
chatObj.UsernameTo = to;
chatObj.UsernameFrom = from;
chatObj.Message = message;
chatObj.Sent = DateTime.Now;
chatObj.Recieved = false;
Chat.Insert(chatObj);
Response.Write("1");
Response.End();
}
private void closeChat()
{
int chatbox = 1; //value of chatbox
((ArrayList)(Session["openChatBoxes"]))[chatbox] = null;
Response.Write("1");
}
private void startChatSession()
{
string items = "";
if (Session["openChatBoxes"] != null)
{ // Check the Session variable
ArrayList openChatBoxesArray = (ArrayList)(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();
}
}
}