zoukankan      html  css  js  c++  java
  • 基于page的简单页面推送技术

    我们可以先看下简单效果,打开2个页面可以看到推送效果

    服务端我们只需要下面一个方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class pagepush : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected override void Render(HtmlTextWriter output)
        {
           
            string msg = "";
            while (true)//构建一个循环
            {
                if (this.Context.Application["message"] != null)//通过context构建消息
                {
                    msg = this.Context.Application["message"].ToString();
                }
                //以js形式向客户传送消息,这里向客户端推送时间
                string str = "<script >window.parent.showmeg0('" + DateTime.Now.ToLongTimeString() + "    " + msg + "')</script>";
                this.Context.Response.Write(str);//向客户端输出信息
                this.Context.Response.Flush();//让服务端所有消息响应客户端
                System.Threading.Thread.Sleep(1000);
            }
        }
    
    }

    看下客户端如何获取数据:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>异步页面推送技术</title>
    
        <script type="text/javascript">
            function showmeg0(str) {
                window.document.getElementById("div0").innerText = str;//传输数据
            }
            function onload() {
                var hidedom0 = new ActiveXObject("htmlfile");//构建一个虚拟页面与服务器连接
                hidedom0.open();//打开虚拟页面
                var subdiv0 = hidedom0.createElement("div");
                hidedom0.appendChild(subdiv0);//向虚拟页面中创建一个div
                hidedom0.parentWindow.showmeg0 = showmeg0;//作为连接
                subdiv0.innerHTML = "<iframe src='pagepush.aspx'></iframe>";
                hidedom0.close();
    
            }
            onload(); 
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="float: left">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="推送msg" />
            <br />
             <h3>客户端要和服务端进行推送,必须要建立一个通道,具体我们可以跟踪代码看下具体传输过程,这种方式并不好,性能差,只适合简单的处理,这里只是演示简单的推送</h3>
            <br />同步Page页面推送时间:<br />
           
            <div id="div0">
            </div>
           
        </div>
       
        </form>
    </body>
    </html>

    具体可以自己做个一个看看效果demo:http://files.cnblogs.com/BABLOVE/%E5%9F%BA%E4%BA%8EPage%E7%9A%84%E5%BC%82%E6%AD%A5%E9%A1%B5%E9%9D%A2%E6%8E%A8%E9%80%81%E6%8A%80%E6%9C%AF.rar

  • 相关阅读:
    Bootstrap导航条
    Bootstrap导航
    Bootstrap输入框组
    Bootstrap按钮式下拉菜单
    Bootstrap按钮组
    Bootstrap下拉菜单
    Bootstrap辅助类
    Bootstrap栅格系统
    Bootstrap学习目录
    Bootstrap图标
  • 原文地址:https://www.cnblogs.com/BABLOVE/p/3269062.html
Copyright © 2011-2022 走看看