码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET零碎

时间:2015-09-14 01:57:26      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

1  ServerPush

    ServerPush表示服务器端推送,实际是浏览器一直请求服务器,服务器一直等待 直到找到一条数据后立即跳出while返回页面。

技术分享
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="file/jquery-1.11.2.js"></script>
    <script type="text/javascript">
        //登录,一直请求服务器推送信息
        var recv = function() {
            var me = $(#me).val().trim();
            $.ajax({
                type: post,
                url: ServerPushChat.ashx,
                data: { me: me, action: receive },
                success: function (data) {
                    $(#dv).append($(<p> + data.data.FROMUSERNAME + 对我说: + data.data.MSG + </p>));
                    //无论成功还是失败,都需要再次向服务器请求‘给哥一条消息’
                    recv();
                },
                error: function () {
                    //alert(‘服务器错误‘);
                    recv();
                }
            });
        }

        $(function () {
            //点击登录,使服务器用serverPush推送信息
            $(#btnLogin).click(function () {
                recv();
                $(this).attr(disabled, disabled);
            });

            //发送,发送给服务器信息
            $(#btnSend).click(function () {
                var me = $(#me).val().trim();
                var toUserName = $(#toUserName).val().trim();
                var msg = $(#msg).val().trim();
                $.ajax({
                    type: post,
                    url: ServerPushChat.ashx,
                    data: { me: me, toUserName: toUserName, msg:msg,action: send },
                    success: function (data) {
                        $(#dv).append($(<p>我对 + data.data.TOUSERNAME + 说: + data.data.MSG + </p>));
                    },
                    error: function () {
                        alert(服务器错误);
                    }
                });
            });
        })
    </script>
</head>
<body>
    <div>
        我:<input type="text" id="me" /><input type="button" id="btnLogin" value="登录" /><br />
        你:<input type="text" id="toUserName" /><br />
        内容:<input type="text" id="msg" /><input type="button" id="btnSend" value="发送" /><br />
    </div>
    <div id="dv">
    </div>
</body>
</html>
ServerPushChat.html
技术分享
using Console_Core.BLL;
using Console_Core.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Script.Serialization;

namespace Web_Cassini.Day6
{
    /// <summary>
    /// ServerPushChat 的摘要说明
    /// </summary>
    public class ServerPushChat : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            if (action == "receive")
            {
                #region 请求服务器一直推送信息
                string me = context.Request["me"];
                //验证 --
                while (true)
                {
                    List<object> list = new MyORM_BLL().SelectModelByField(typeof(TC_SERVERPUSHCHAT), "TOUSERNAME=‘" + me + "");
                    if (list.Count <= 0)
                    {
                        Thread.Sleep(500);
                        continue;
                    }
                    else
                    {
                        TC_SERVERPUSHCHAT chat = list[0] as TC_SERVERPUSHCHAT;
                        bool flag = new MyORM_BLL().DeleteModelById(typeof(TC_SERVERPUSHCHAT), (int)chat.ID); //把客户接收到的数据删除
                        context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "ok", data = chat, msg = "接收到一条数据" }));
                        break; //接收到一条数据 立即跳出while 返回页面
                    }
                } 
                #endregion
            }
            else if (action == "send")
            {
                string me = context.Request["me"];
                string toUserName = context.Request["toUserName"];
                string msg = context.Request["msg"];
                //验证 ---
                TC_SERVERPUSHCHAT chat=new TC_SERVERPUSHCHAT();
                chat.FROMUSERNAME=me;
                chat.TOUSERNAME=toUserName;
                chat.MSG=msg;
                bool flag = new MyORM_BLL().InsertModel(chat, "SE_TC_STUDENT");
                context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "ok", data = chat, msg = "发送成功" }));
            }
            else
            {
                throw new Exception("action错误:"+action);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
ServerPushChat.ashx

技术分享技术分享

 

ASP.NET零碎

标签:

原文地址:http://www.cnblogs.com/adolphyang/p/4806001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!