ASP.NET实现的消息推送,一个简单的聊天室,基于长连接实现。
实现了:
用户的登录与退出
在线用户列表的获取
消息的推送与接收
基于这个很容易实现站内IM即时通讯系统,核心WebIM.js代码如下(下载源码,运行Default.aspx登录即可):
/// <reference path="jquery-1.3.2.min.js" >
//$(document).ready(function () {
//状态,代表是否登录
//var _logined = false;
//登录,登录成功后,获取在线用户列表,
function login() {
//$.post("comet_broadcast.asyn", { action: 'login', uid: $("#txtLoginID").val() },
$.post("comet_broadcast.asyn", { action: 'login', uid: strUid },
function (data, status) {
if (data == "OK") {
_logined = true;
getuserlist();
//增加页面跳转
/*var strUrl = '\SendInfo.aspx?strUid=' + $("#txtLoginID").val();
window.open(strUrl);
*/
}
else {
alert(data);
}
});
}
//获取在线用户列表,获取列表后,进入消息等待
function getuserlist() {
//$.post("comet_broadcast.asyn", { action: 'getuserlist', uid: $("#txtLoginID").val() },
$.post("comet_broadcast.asyn", { action: 'getuserlist', uid: strUid },
function (data, status) {
//alert('getuserlist' + data);
var result = $("#divResult");
result.html(result.html() + "<br/>" + "用户列表:" + data);
wait();
});
}
//退出
function logout() {
//$.post("comet_broadcast.asyn", { action: 'logout', uid: $("#txtLoginID").val() },
$.post("comet_broadcast.asyn", { action: 'logout', uid: strUid },
function (data, status) {
_logined = false;
alert(data);
}
);
}
//消息等待,接收到消息后显示,发起下一次的消息等待
function wait() {
//$.post("comet_broadcast.asyn", { action: 'connect', uid: $("#txtLoginID").val() },
$.post("comet_broadcast.asyn", { action: 'connect', uid: strUid },
function (data, status) {
/*
var result = $("#divResult");
result.html(result.html() + "<br/>" + "用户列表:" + data);
*/
//2.窗口
/*
var strSubData = GetSubStrBySpecChar(data, ':');
if (strSubData == 'Msgs') {
new Ext.ux.ToastWindow({
title: '提示窗口',
html: data,
iconCls: 'error'
}).show(document);
}
else if (strSubData == 'Users') {
//暂时不处理
}
*/
new Ext.ux.ToastWindow({
title: '提示窗口',
html: data,
iconCls: 'error'
}).show(document);
//服务器返回消息,再次建立连接
if (_logined) {
wait();
}
}, "html");
}
//function send() {
// //向comet_broadcast.asyn发送请求,消息体为文本框content中的内容,请求接收类为AsnyHandler
// $.post("comet_broadcast.asyn", { content: $("#content").val() });
// //清空内容
// $("#content").val("");
//}
//function wait() {
// $.post("comet_broadcast.asyn", { content: "-1" },
// function(data, status) {
// var result = $("#divResult");
// result.html(result.html() + "<br/>" + data);
// //服务器返回消息,再次立连接
// wait();
// }, "html"
// );
//}
////初始化连接
// wait();
//$("#btnSend").click(function() { send(); });
//$("#content").keypress(function(event) {
// if (event.keyCode == 13) {
// send();
// }
//});
//附加事件
///$("#btnLogin").click(function () { if ($("#txtLoginID").val() == '') alert('空'); login(); });
//$("#btnLogout").click(function () {
// logout();
//});
/***********
*********************消息发送部分***************************
************/
function send() {
//$.post("comet_broadcast.asyn", { action: 'sendmsg', uid: $("#txtLoginID").val(), content: $("#content").val() },
$.post("comet_broadcast.asyn", { action: 'sendmsg', uid: strUid, content: $("#content").val() },
function (data, status) {
/*
var result = $("#divResult");
result.html(result.html() + "<br/>" + "已发消息:" + data);
*/
//发送方页面提示
//潜规则:暂时不处理
/*
//2.窗口
new Ext.ux.ToastWindow({
title: '提示窗口',
html: data,
iconCls: 'error'
}).show(document);
*/
}, "html"
);
//向comet_broadcast.asyn发送请求,消息体为文本框content中的内容,请求接收类为AsnyHandler
//$.post("comet_broadcast.asyn", { content: $("#content").val() });
//清空内容
$("#content").val("");
};
/**
* 获取字符串中某个特殊字符首次出现的位置之前的子串
*/
function GetSubStrBySpecChar(strConnStr,strSplit){
var arrStr = strConnStr.split(strSplit);
var strSubStr = arrStr[0];
return strSubStr;
}
/*
$("#btnSend").click(function () { send(); })
$("#content").keypress(function (e) {
var keyCode = null;
if (e.which)
keyCode = e.which;
else if (e.keyCode)
keyCode = e.keyCode;
if (keyCode == 13) {
send();
return false;
}
return true;
});*/
//获取在线用户列表,获取列表后,进入消息等待
/*
function getuserlist_send() {
$.post("comet_broadcast.asyn", { action: 'getuserlist', uid: strUid },
function (data, status) {
//alert('getuserlist' + data);
var result = $("#divResult");
result.html(result.html() + "<br/>" + "用户列表:" + data);
wait();
});
}*/
//});
该文章在 2015/1/31 15:49:54 编辑过