zoukankan      html  css  js  c++  java
  • webfrom B/S 消息推送

    转哪的忘了,这个适用于不支持html5浏览器,只是写了个大概,至于BUG和需求啥地,需要自己慢慢鼓捣了。

    myAsynResult.cs:

    /// <summary>
        /// 请求的客户端
        /// </summary>
        public class myAsynResult : IAsyncResult
        {
            #region IAsyncResult接口      
            public object AsyncState
            {
                get
                {
                    //throw new NotImplementedException();
                    return null;
                }
            }
            public WaitHandle AsyncWaitHandle
            {
                get
                {
                    //throw new NotImplementedException();
                    return null;
                }
            }
            public bool CompletedSynchronously
            {
                get
                {
                    //throw new NotImplementedException();
                    return false;
                }
            }
            public bool IsCompleted
            {
                get
                {
                    //throw new NotImplementedException();
                    return false;
                }
            }
            #endregion
    
            bool _IsCompleted = false;
            private HttpContext context;
            private AsyncCallback cb;
            private object extraData;
            public myAsynResult(HttpContext context, AsyncCallback cb, object extraData)
            {
                this.context = context;
                this.cb = cb;
                this.extraData = extraData;
            }
    
            public string content { get; set; }
            public void Send(object data)
            {
                context.Response.Write(this.content);
                if (cb != null)
                {
                    cb(this);
                }
                _IsCompleted = true;
            }
        }

    Model_Requery.cs:

    /// <summary>
        /// 请求
        /// </summary>
        public class Model_Requery
        {
            public RequeryType type { get; set; }
            /// <summary>
            /// 内容
            /// </summary>
            public string content { get; set; }
    
        }
    
        public enum RequeryType
        {
            登录 = 0,
            个人 = 1,
            全部 = 2
        }

    Messages.cs:

    /// <summary>
        /// 消息
        /// </summary>
        public class Messages
        {
            List<myAsynResult> clients = new List<myAsynResult>();
            private static readonly Messages _Instnce = new Messages();
            private Messages() { }
            public static Messages Instance()
            {
                return _Instnce;
            }
            /// <summary>
            /// 消息信息处理
            /// </summary>
            /// <param name="m"></param>
            /// <param name="asyncResult"></param>
            public void AddMessage(Model_Requery m, myAsynResult asyncResult)
            {
                //当传入的内容为"-1"时,表示为建立连接请求,即为了维持一个从客户端到服务器的连接而建立的连接
                //此时将该连接保存到List<myAsynResult> clients中,待再有消息发送过来时,该连接将不会被遍历,并且会将该连接输出内容后,结束该连接
                switch (m.type)
                {
                    case RequeryType.登录:
                        clients.Add(asyncResult);
                        break;
                    case RequeryType.个人:
                        break;
                    case RequeryType.全部:
                        //将当前请求的内容输出到客户端
                        asyncResult.content = m.content;
                        asyncResult.Send(null);
    
                        foreach (myAsynResult result in clients)
                        {
                            result.content = m.content;
                            result.Send(null);
                        }
    
                        //清空所有缓存
                        clients.Clear();
                        break;
                }
            }
        }

    MessagePushController.ashx:

    /// <summary>
        /// MessagePushController 的摘要说明
        /// </summary>
        public class MessagePushController : IHttpAsyncHandler
        {
    
            public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
            {
                //myAsynResult为实现了IAsyncResult接口的类,当不调用cb的回调函数时,该请求不会返回到给客户端,会一直处于连接状态
                myAsynResult asyncResult = new myAsynResult(context, cb, extraData);
                int type = 0;
                int.TryParse(context.Request["type"], out type);
                RequeryType requerytype;
                switch (type)
                {
                    case 1: requerytype = RequeryType.个人; break;
                    case 2: requerytype = RequeryType.全部; break;
                    default: requerytype = RequeryType.登录; break;
                }
                string content = context.Request["contents"];
                Model_Requery model = new Model_Requery()
                {
                    type = requerytype,
                    content = content
                };
                Messages.Instance().AddMessage(model, asyncResult);
                return asyncResult;
            }
    
            public void EndProcessRequest(IAsyncResult result)
            {
                //throw new NotImplementedException();
            }
    
            public void ProcessRequest(HttpContext context)
            {
    
            }
    
    
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

    Demo.aspx

    <!DOCTYPE html>
    
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="/scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
        <title></title>
        <script type="text/javascript">
            $(function () {
                $('#send').click(function () {
                    $.ajax({
                        url: "MessagePushController.ashx",
                        type: 'post',
                        data: {
                            type: 2,
                            contents: $('#txt_client').val()
                        }
                    });
                });
                ConnectServer();
                function ConnectServer() {
                    /// <summary>
                    /// 与服务器建立连接
                    /// </summary>
                    $.ajax({
                        url: 'MessagePushController.ashx',
                        data: { type: 0, contents: "0" },
                        type: 'post',
                        success: function (data, status) {
                            $('#txt_server').val(data);
                            ConnectServer();
                        }
                    });
                }
            });
        </script>
    </head>
    <body>
        <div>
            <span>服务器</span>
            <div>
                <textarea id="txt_server"></textarea>
            </div>
        </div>
        <div>
            <span>发送信息</span>
            <div>
                <input type="text" id="txt_client"></input>
            </div>
            <div>
                <input type="button" id="send" value="发送" />
            </div>
        </div>
    </body>
    </html>
  • 相关阅读:
    【已解决】github中git push origin master出错:error: failed to push some refs to
    好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
    THINKPHP 5.0目录结构
    thinkphp5.0入口文件
    thinkphp5.0 生命周期
    thinkphp5.0 架构
    Django template
    Django queryset
    Django model
    Python unittest
  • 原文地址:https://www.cnblogs.com/OleRookie/p/5382756.html
Copyright © 2011-2022 走看看