CometController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using CometMvcDemo.Models; namespace CometMvcDemo.Controllers { public class CometController : AsyncController { private AsyncCallback cb; object extraData; HttpContext context; // // GET: /Comet/ public ActionResult Index() { var strUid = Request["strUid"]; ViewData["strUid"] = strUid; return View(); } //LongPolling Action 1 - 处理客户端发起的请求 public void LongPollingAsync() { //计时器,5秒种触发一次Elapsed事件 System.Timers.Timer timer = new System.Timers.Timer(5000); //告诉ASP.NET接下来将进行异步操作 AsyncManager.OutstandingOperations.Increment(); //订阅至计时器的Elapsed事件 timer.Elapsed += (sender, e) => { //保存将要传递给LongPollingCompleted的参数 AsyncManager.Parameters["now"] = e.SignalTime; //告诉ASP.NET异步操作已完成,进行LongPollingCompleted方法的调用 AsyncManager.OutstandingOperations.Decrement(); }; //启动计时器 timer.Start(); } //LongPolling Action 2 - 异步处理完成,向客户端发送响应 /* public ActionResult LongPollingCompleted(DateTime now) { return Json(new { d = now.ToString("MM-dd HH:mm:ss ") + "-- Welcome to cnblogs.com!" }, JsonRequestBehavior.AllowGet); } */ /// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="cb"></param> /// <param name="extraData"></param> /// <returns></returns> public void LongPollingCompleted() { //var oContext = HttpContext.Profile; context = System.Web.HttpContext.Current; string _UID = context.Request.Params["uid"]; WebIMClientAsyncResult _AsyncResult = new WebIMClientAsyncResult(context,cb,null); string _Content = context.Request.Params["content"]; string _Action = context.Request.Params["action"]; if (_Action == "login") { _UID = context.Request.Params["uid"]; _AsyncResult.LoginID = _UID; WebIMMessageHandler.Instance().Login(_UID, _AsyncResult); } else if (_Action == "logout") { _AsyncResult.LoginID = _UID; WebIMMessageHandler.Instance().Logout(_UID, _AsyncResult); } else if (_Action == "connect") { _AsyncResult.LoginID = _UID; WebIMMessageHandler.Instance().Connect(_AsyncResult); } else if (_Action == "getuserlist") { _AsyncResult.LoginID = _UID; WebIMMessageHandler.Instance().GetUserList(_AsyncResult); } //增加消息发送 else if (_Action == "sendmsg") { _AsyncResult.LoginID = _UID; //WebIMMessageHandler.Instance().GetUserList(_AsyncResult); //调用 WebIMMessageHandler.Instance().AddMessage(_Content, _AsyncResult); } //调用 //WebIMMessageHandler.Instance().AddMessage(_Content, _AsyncResult); //return _AsyncResult; //return Json(_AsyncResult.Content,JsonRequestBehavior.AllowGet); } } }
WebIM.js
//登录,登录成功后,获取在线用户列表, function login() { //var strUrl = '/Comet/LongPolling'; //$.post("comet_broadcast.asyn", { action: 'login', uid: strUid }, $.post('/Comet/LongPolling', { action: 'login', uid: strUid }, function (data, status) { if (data == "OK") { _logined = true; getuserlist(); } else { //alert(data); Ext.Msg.alert("提示",data); } }); } //获取在线用户列表,获取列表后,进入消息等待 function getuserlist() { $.post('/Comet/LongPolling', { action: 'getuserlist', uid: strUid }, function (data, status) { //alert('getuserlist' + data); var result = $("#divResult"); result.html(result.html() + "<br/>" + "用户列表:" + data); wait(); }); } //退出 function logout() { $.post('/Comet/LongPolling', { action: 'logout', uid: strUid }, function (data, status) { _logined = false; alert(data); } ); } //消息等待,接收到消息后显示,发起下一次的消息等待 function wait() { $.post('/Comet/LongPolling', { 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') { //暂时不处理 } else { new Ext.ux.ToastWindow({ title: '提示窗口', html: data, iconCls: 'error' }).show(document); } //服务器返回消息,再次建立连接 if (_logined) { wait(); } */ new Ext.ux.ToastWindow({ title: '提示窗口', html: 'HAHAH', iconCls: 'error' }).show(document); //服务器返回消息,再次建立连接 if (_logined) { wait(); } }, "html"); } //附加事件 ///$("#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/LongPolling', { 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" ); //清空内容 $("#content").val(""); }; /** * 获取字符串中某个特殊字符首次出现的位置之前的子串 */ function GetSubStrBySpecChar(strConnStr, strSplit) { var arrStr = strConnStr.split(strSplit); var strSubStr = arrStr[0]; return strSubStr; }