AJax+ASP.Net+mssql實現的AJax無刷新聊天室,支持Html web編輯器。供大家參考下。不過有個小問題,就是Firefox下如果使用快捷鍵ctrl+enter發送信息時,如果不延時調用發送函數,會彈出ff的下載對話框,奇怪。
JScript codeif(e.ctrlKey&&e.keyCode==13){
e.PReventDefault();
e.stopPropagation();
setTimeout("Showbo.Chat.send()",50);//在ff中馬上使用alert提示時盡然彈出下載工具,奇怪.
return false;}
},false);
為了加快速度,使用了存儲過程。
ASP及PHP版本請看下面的文章
AJax+ASP+mssql無刷新聊天室
AJax+PHP+mssql無刷新聊天室
要使用不同的動態頁面,只需要更改lib.JS文件中RequstUrl變量的值即可。
完整示例下載
效果如下
內容長度限制,所以就不貼出所有的代碼了,要看全部代碼請下載示例或者查看下面這篇文章
AJax+ASP.Net+mssql無刷新聊天室
下面貼出C#的主要的代碼,其他版本請查看上面的鏈接
AJax.cs
C# codeusing System;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClIEnt;
public class AJax
{
private static bool IsNull(string v)
{
if (v == null || v.Trim() == "") return true;
else return false;
}
private static string JS(string v)
{
return v.Replace("'", "'");
}
public static string Login()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "";
string UserName = Request.Form["nn"];
if (IsNull(UserName))
{
rStr = "success:false,err:'昵稱不能為空!'";
}
else if (UserName.Length > 20)
{
rStr = "success:false,err:'昵稱不能超過20個字符!'";
}
else
{
string UserId = "", Key = "";
SqlConnection cn = new SqlConnection(ConfigurationSettings.APPSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("AJaxLogin", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50));
cm.Parameters["@UserName"].Value = UserName;
//==========輸出參數
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Direction = ParameterDirection.Output;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
UserId = cm.Parameters["@UserId"].Value.ToString().Trim();
Key = cm.Parameters["@UserKey"].Value.ToString().Trim();
if (UserId == "-1") rStr = @"success:false,err:'發生錯誤,請稍後再試!'";
else if (UserId == "0") rStr = @"success:false,err:'已經存在此用戶昵稱,請修改您的昵稱!'";
else rStr += "success:true,UserId:'" + UserId + "',Key:'" + Key + "'";
cm.Dispose();
}
catch (Exception e)
{
rStr = @"success:false,err:'原因n" + JS(e.Message) + "'";
}
cn.Close();
}
return rStr;
}
public static string Logout()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "", UserId = Request.Form["uid"], Key = Request.Form["key"];
if (IsNull(UserId) || IsNull(Key)) return "success:false,err:'用戶信息丟失!'";
SqlConnection cn = new SqlConnection(ConfigurationSettings.APPSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("AJaxLogout", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Value = UserId;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Value = Key;
cm.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
cm.Parameters["@Result"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
if(cm.Parameters["@UserId"].Value.ToString().Trim()=="0")rStr = "success:false,err:'用戶信息不存在!'";
else rStr="success:true";
cm.Dispose();
}
catch (Exception e)
{
}
cn.Close();
return rStr;
}
public static string Say()
{
HttpRequest Request = HttpContext.Current.Request;
string From = Request.Form["from"], To = Request.Form["to"]
, Key = Request.Form["key"], Msg = Request.Form["ct"], rStr = "";
if (IsNull(From) || IsNull(Key) || IsNull(To) || IsNull(Msg)) rStr = "success:false,err:'信息傳遞不完整!'";
else
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.APPSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("AJaxSay", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Value = Key;
cm.Parameters.Add(new SqlParameter("@From", SqlDbType.NVarChar, 18));
cm.Parameters["@From"].Value = From;
cm.Parameters.Add(new SqlParameter("@To", SqlDbType.NVarChar, 18));
cm.Parameters["@To"].Value = To;
cm.Parameters.Add(new SqlParameter("@Msg", SqlDbType.NVarChar, 800));
cm.Parameters["@Msg"].Value = Msg;
cm.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
cm.Parameters["@Result"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();
if (cm.Parameters["@Result"].Value.ToString() == "0") rStr = "sucess:false,err:'發表失敗!n原因:接收者已經不存在!'";
else rStr = "success:true";
cm.Dispose();
}
catch (Exception e)
{
rStr = "sucess:false,err:'發表失敗!原因n" + JS(e.Message) + "'";
}
cn.Close();
}
return rStr;
}
public static string ReadUser()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "", UserId = Request.Form["uid"];
if (IsNull(UserId)) rStr += "success:false,err:'用戶id丟失!'";
else
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.APPSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("AJaxReadUser", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Value = UserId;
string j = "";
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read()) j += ",{id:'" + dr[0] + "',nn:'" + JS(dr[1].ToString()) + "'}";
dr.Close();
cm.Dispose();
rStr = "success:true,data:[" + (j == "" ? "" : j.Substring(1)) + "]";
}
catch (Exception e)
{
rStr = @"success:false,err:'發生如下錯誤n" + JS(e.Message) + "'";
}
cn.Close();
}
return rStr;
}
public static string Read()
{
HttpRequest Request = HttpContext.Current.Request;
string rStr = "";
string UserId = Request.Form["uid"], Key = Request.Form["key"];
if (IsNull(UserId) || IsNull(Key)) rStr = "success:false,err:'用戶信息丟失!'";
else
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.APPSettings["db"]);
cn.Open();
try
{
SqlCommand cm = new SqlCommand("AJaxRead", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@UserId", SqlDbType.NVarChar, 18));
cm.Parameters["@UserId"].Value = UserId;
cm.Parameters.Add(new SqlParameter("@UserKey", SqlDbType.NVarChar, 5));
cm.Parameters["@UserKey"].Value = Key;
SqlDataReader dr = cm.ExecuteReader();
string j = "";
while (dr.Read()) j += ",'" + JS(dr[0].ToString()) + "'";
dr.Close();
cm.Dispose();
rStr = "success:true,data:[" + (j == "" ? "" : j.Substring(1)) + "]";
}
catch (Exception e)
{
rStr = "success:false,err:'發生以下錯誤" + JS(e.Message) + "'";
}
cn.Close();
}
return rStr;
}