Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6969525
  • 博文数量: 701
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12021
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院架构师,专注企业数字化各个方面,MES/ERP/CRM/OA、物联网、传感器、大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(701)

分类: 项目管理

2011-03-23 11:39:25

很多的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" %>





无标题页







您有条短消息





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
{
///
/// $codebehindclassname$ 的摘要说明
///

[WebService(Namespace = "")]
[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

转自:http://www.cnblogs.com/shengtianlong/archive/2010/09/09/1822578.html

阅读(10490) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~