diskman硬盘修复工具:asp.net 即时消息提示,网页 短消息即时提醒 asp.net 简单实现
来源:百度文库 编辑:偶看新闻 时间:2024/04/30 06:53:24
很多的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 = "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
这里介绍一种客户端的方法,简单实现。
主要的表:
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 = "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
ASP网页提示语法错误
用ASP.NET,但点击网页预览老提示MDAC要2.6以上
运行asp.net程序为什么提示下载~
怎么在最短的时间内学好ASP.net
谁会用ASP.NET做网页.
ASP.NET C#如何获得网页URL
如何用DREAMWEAVER ASP.NET 做网页
如何在ASP.NET中实现弹出提示窗口
如何修改asp.net里的404错误提示?
ASP。NET
ASP.NET?
制asp.net网页,数据库绑定时无表
用ASP.NET做网页一般用什么工具软件
关于 ASP.NET 网页的 Request.Form 的问题?
在asp.net中如何做退出网页按钮?
asp.net关于网页路径的基本问题
asp.net关于网页路径的基本问题
dreamweaver2004中制作ASP.NET动态网页中数据库连接问题
我要做asp.net网页,如何入手?
我用asp.net-vb编写网页出现的问题
用asp.net开发网页有什么优点?
用asp.net和jsp编写网页程序,哪种更有发展前景?
如何防止ASP,JSP,.NET等等网页被下载?
asp.net(C#)中使用DataGrid出现网页错误怎么办?