日期:2014-05-17  浏览次数:21140 次

asp.net 基于ajax 实现即时消息

很多的sns网站都提供了短消息功能。而且,如果我们在线的话会很快的收到好友的短消息。
这里介绍一种客户端的方法,简单实现。

主要的表:
user
??? :Uid UName Password 三个字段
Message
??? :Mid, SenderId, ReceiverId, State, Detail(SenderId和 ReceiverId)都是外键且对应user表中的Uid。


主要的思路很简单:用js每隔五秒钟发送一次ajax请求,获取当前用户在Message表中State为未读取(这里约定为数字1)且ReceverId为当前用户ID的Message 记录的数量。

页面的代码:
<%@ Page Language="C#" CodeBehind="Default.aspx.cs" Inherits="MIDemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>无标题页</title>
??? <!-- 两个js脚本文件-->
??? <script type="text/javascript" src="SqlHelp/jquery-1.3.2.js"></script>
??? <script type="text/javascript" src="SqlHelp/GetMessageCount.js"></script>
</head>
<body>
??? <form id="form1" runat="server">
??? <div style=" border-color:Green; border-style:solid; margin-top:100px; margin-left:300px; width:300px; height:50px; text-align:center;">
??????? 您有<input type="text" value="0" id="messageCount"/><a href="ShowMessage.aspx">条短消息</a>
??? </div>
??? </form>
</body>
</html>

js代码:这里用到了Jquery框架,不再赘述,网上有很多的资料。
GetMessageCount.js
//------GetMessageCount.js Begin----------------------
if(!GetMessageCount){
??? var GetMessageCount = {};
}

$(document).ready(
??? function(){
??????? GetMessageCount.FindMessage();
??? }
);

GetMessageCount.FindMessage = function(){
??????? $.ajax({
?????????? //处理ajax请求
?????????? url:'FindNewMessage.ashx',
?????????? // 当前用户的ID,这里图省事就省略了,直接写死为 1,
?????????? //实际使用过程中可以从session中获取 。。。。
?????????? data:{Uid:1},
?????????? cache: false,
?????????? //回调函数返回未读短信数目
?????????? success: function(response)
?????????? {
????????????? $('#messageCount').val(response);
?????????? },
?????????? error:function(data)
?????????? {
?????????????? alert("加载失败");
?????????? }
?????? });
?????? //每隔5 秒递归调用一次,刷新未读短信数目
?????? window.setTimeout(GetMessageCount.FindMessage,5000);核心语句
}
//------GetMessageCount.js End----------------------

到了这里,贴出处理ajax请求页面的代码,非常简单
FindNewMessage.ashx

//----------------'FindNewMessage.ashx Begin
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace MIDemo
{
??? /// <summary>
??? /// $codebehindclassname$ 的摘要说明
??? /// </summary>
??? [WebService(Namespace = "http://tempuri.org/")]
??? [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
??? public class FindNewMessage : IHttpHandler
??? {

??????? public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/plain";
????????????? //就这一句代码,获取未读短信的数量,返回页面
????????????? //后台的sql代码就省略了
??????????? int count = SqlHelp.SqlHelp.GetUnreadMessageCount(Convert.ToInt32(context.Request["Uid"]));
??????????? //返回页面
??????????? context.Response.Write(count);
??????? }

??????? public bool IsReusable
??????? {
??????????? get
??????????? {
??????????????? return false;
??????????? }
??????? }
??? }
}

//----------------'FindNewMessage.ashx End

?