问题描述
- comet实现服务器推送技术出现了一些问题
-
如题
自己写的基于comet服务器推送信息的网站,自己调试的时候没有问题,数据能够更改。
但是 发布到IIS上, 用别的电脑访问的时候,数据不能更改,感觉没有运行代码。
这两台电脑的 防火墙都是关闭的~
js$(function () { a(); } function a(){ $.ajax({ url: "../../NetAnomaly.ashx?"+Math.random(), type: "post", data: { "type": "connection" }, success: function (data) { if (data=="1") { a(); } a(); }, error: function () { a(); } }) }
cs
public class NetAnomaly1 : IHttpAsyncHandler { public static string name = HttpContext.Current.User.Identity.Name; public void ProcessRequest(HttpContext context) { } public bool IsReusable { get { return false; } } public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { //web server 调用BeginProcessRequest 开始异步处理http请求 IAsyncResult ar = new NetAsyncResult(context, cb, extraData); if (context.Request["type"].Equals("connection")) { chatManager.Start_Long_Connection(ar, name); } return ar; } public void EndProcessRequest(IAsyncResult result) { //web server 调用EndProcessRequest结束 异步处理http请求 一次http连接结束 } } /// <summary> /// 唯一标示一个异步请求http请求 /// </summary> class NetAsyncResult : IAsyncResult { HttpContext _context; //与当前http请求相关的context AsyncCallback _cb; //异步处理的回调方法 object _extraData; // 开启异步处理时 传进来的附加参数 bool _isCompleted; //异步过程是否完成 public NetAsyncResult(HttpContext context, AsyncCallback cb, object extraData) { _context = context; _cb = cb; _extraData = extraData; } //向客户端发送 数据 public void SendMsg(string msg) { try { _context.Response.ContentType = "text/html"; _context.Response.Write(msg); try { if (_cb != null) { //这步非常关键 该操作告诉web server 结束本次http链接(即server-borwser的 一次http连接结束) _cb(this); } } catch (Exception ex) { throw ex.message; } } finally { _isCompleted = true; } } public object AsyncState { get { return null; } } public System.Threading.WaitHandle AsyncWaitHandle { get { return null; } } public bool CompletedSynchronously { get { return false; } } public bool IsCompleted { get { return _isCompleted; } } } class chatClient { static IAsyncResult _netAsyncResult; static object _syncObj = new object(); static DateTime _last_connect_time = DateTime.Now; public chatClient(IAsyncResult ar) { _netAsyncResult = ar; _last_connect_time = DateTime.Now; } public static bool CheckState() { if (DateTime.Now.AddSeconds(-30) > _last_connect_time) { //30秒没有新的长连接建立 则认为 客户端断开 return false; } if (DateTime.Now.AddSeconds(-15) > _last_connect_time) { //每15秒发送一次心跳包 强制browser重新建立连接 lock (_syncObj) { (_netAsyncResult as NetAsyncResult).SendMsg("1"); } } return true; } public void UpdateAsyncResult(IAsyncResult ar) //重新建立http长连接 { lock (_syncObj) { _netAsyncResult = ar; _last_connect_time = DateTime.Now; (_netAsyncResult as NetAsyncResult).SendMsg("1"); } } } class chatManager { static object _syncObj = new object(); static string name = string.Empty; static chatManager() { StartCheckState(); } /// <summary> /// 监测在线用户的连接状态 /// </summary> private static void StartCheckState() { BackgroundWorker b = new BackgroundWorker(); b.DoWork += new DoWorkEventHandler(b_DoWork); b.RunWorkerAsync();//开始执行后台操作 } static void b_DoWork(object sender, DoWorkEventArgs e) { while (true) { lock (_syncObj) { try { //判断当前长链接是否建立 if (!chatClient.CheckState()) { //具体的操作 break; } } catch (Exception ex) { throw ex; } } Thread.Sleep(1000); } } /// <summary> /// browser端 开始建立一个http长连接 /// </summary> /// <param name="ar"></param> public static void Start_Long_Connection(IAsyncResult ar, string uname) { lock (_syncObj) { name = uname; chatClient client = new chatClient(ar); client.UpdateAsyncResult(ar); } } }
这代码是我按照一个例子更改的
链接地址:http://www.cnblogs.com/xiaozhi_5638/p/3722017.html
解决方案
3GoEasy也可以实现这个功能,自己感觉GoEasy要比comet简单易懂些,GoEasy推送专注做web实时推送,服务稳定,中英文文档齐全,是个很不错的选择!官网:https://goeasy.io
时间: 2024-12-09 13:05:06