ASP.NET實現(xiàn)的消息推送,一個簡單的聊天室,基于長連接實現(xiàn)。
實現(xiàn)了:
用戶的登錄與退出
在線用戶列表的獲取
消息的推送與接收
基于這個很容易實現(xiàn)站內(nèi)IM即時通訊系統(tǒng),核心WebIM.js代碼如下(下載源碼,運行Default.aspx登錄即可):
/// <reference path="jquery-1.3.2.min.js" >
//$(document).ready(function () {
//狀態(tài),代表是否登錄
//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();
//增加頁面跳轉(zhuǎn)
/*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);
}
);
}
//消息等待,接收到消息后顯示,發(fā)起下一次的消息等待
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);
//服務(wù)器返回消息,再次建立連接
if (_logined) {
wait();
}
}, "html");
}
//function send() {
// //向comet_broadcast.asyn發(fā)送請求,消息體為文本框content中的內(nèi)容,請求接收類為AsnyHandler
// $.post("comet_broadcast.asyn", { content: $("#content").val() });
// //清空內(nèi)容
// $("#content").val("");
//}
//function wait() {
// $.post("comet_broadcast.asyn", { content: "-1" },
// function(data, status) {
// var result = $("#divResult");
// result.html(result.html() + "<br/>" + data);
// //服務(wù)器返回消息,再次立連接
// 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();
//});
/***********
*********************消息發(fā)送部分***************************
************/
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/>" + "已發(fā)消息:" + data);
*/
//發(fā)送方頁面提示
//潛規(guī)則:暫時不處理
/*
//2.窗口
new Ext.ux.ToastWindow({
title: '提示窗口',
html: data,
iconCls: 'error'
}).show(document);
*/
}, "html"
);
//向comet_broadcast.asyn發(fā)送請求,消息體為文本框content中的內(nèi)容,請求接收類為AsnyHandler
//$.post("comet_broadcast.asyn", { content: $("#content").val() });
//清空內(nèi)容
$("#content").val("");
};
/**
* 獲取字符串中某個特殊字符首次出現(xiàn)的位置之前的子串
*/
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 編輯過