ajax+asp.net+mssql无刷新聊天室
ajax+asp.net+mssql实现的ajax无刷新聊天室,支持html web编辑器。供大家参考下。
不过有个小问题,就是firefox下如果使用快捷键ctrl+enter发送信息时,如果不延时调用发送函数,会弹出ff的下载对话框,奇怪。
谁能解决这个问题,100分归他,其他100分当作散分。
if(e.ctrlKey&&e.keyCode==13){ e.preventDefault(); e.stopPropagation(); setTimeout("Showbo.Chat.send()",50);//在ff中马上使用alert提示时尽然弹出下载工具,奇怪. return false;} },false);
using 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; }}
function checkInput(event){ event=(event)?event:window.event; //为了兼容FF if (event.keyCode==13||event.charCode==13) { var content=document.getElementById("sendtext").value; if (content!="") { Chat.SendMsg(content); //调用后台方法发送消息 document.getElementById("Main").scrollTop=document.getElementById("Main").scrollHeight; document.getElementById("sendtext").value=""; } else { document.getElementById("sendtext").value=""; } }}
[解决办法]
學習 了
幫你頂哈···
[解决办法]
function keypress(evt)
{
evt = evt ? evt : (window.event ? window.event : null);
if(evt.ctrlKey && evt.keyCode==13)
{
SendMsg();
return false;
}
else if(evt.ctrlKey && evt.keyCode==10)
{
event.keyCode = 13;
}
return true;
}
[解决办法]
接分
[解决办法]
看看
[解决办法]
这个绝对得顶,学习。。
[解决办法]
厉害啊
[解决办法]
这个 支持一下吧
[解决办法]
挺好
[解决办法]
厉害 ,先顶这个了~
[解决办法]
好东西就要收藏
[解决办法]
支持分享,
[解决办法]
<body> <script type="text/javascript"> function isKeyTrigger(e,keyCode){ var argv = isKeyTrigger.arguments; var argc = isKeyTrigger.arguments.length; var bCtrl = false; if(argc > 2){ bCtrl = argv[2]; } var bAlt = false; if(argc > 3){ bAlt = argv[3]; } var nav4 = window.Event ? true : false; if(typeof e == 'undefined') { e = event; } if( bCtrl && !((typeof e.ctrlKey != 'undefined') ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)){ return false; } if( bAlt && !((typeof e.altKey != 'undefined') ? e.altKey : e.modifiers & Event.ALT_MASK > 0)){ return false; } var whichCode = 0; if (nav4) whichCode = e.which; else if (e.type == "keypress" || e.type == "keydown") whichCode = e.keyCode; else whichCode = e.button; return (whichCode == keyCode); } function ctrlEnter(e){ var ie =navigator.appName=="Microsoft Internet Explorer"?true:false; if(ie){ if(event.ctrlKey && window.event.keyCode==13){doSomething();} }else{ if(isKeyTrigger(e,13,true)){doSomething();} } } function doSomething(){alert('Ctrl+Enter being Pressed.'); } </script> <textarea name="message" onkeyup="javascript:return ctrlEnter(event);">Ctrl+Enter Submit test</textarea> </body>
[解决办法]
这个应该和QQ差不多吧
[解决办法]
支持分享。
[解决办法]
学习了
[解决办法]
正需要!!!!!!!!!!!!!!!
[解决办法]
进来学习
------解决方案--------------------
顶!!JF
[解决办法]
顶
[解决办法]
进来接分,顺便问个问题,你是怎么判断用户离开、或下线
[解决办法]
ff
[解决办法]
是不是和火狐自带的下载工具有冲突啊,可能火狐的快捷键就是ctrl+Enter,试试能不能屏蔽火狐的下载工具!我记得用火狐浏览器,如果想下载东西的话,火狐会调用自带的内部下载工具,连迅雷都起不来。我试过屏蔽它而调用迅雷,可以的,你也试试。不过要是客户用的话就麻烦了,他们那里晓得去屏蔽狐火的下载工具啊,所以完全之策就是更改热键ctrl+Enter,可以改为直接敲Enter
[解决办法]
很有意义!!!
[解决办法]
看过了 楼主的代码 很不错
[解决办法]
收藏,学习
[解决办法]