新聞中心
今天突發(fā)奇想,想研究一下服務端內(nèi)容有變化,如何引起客戶端的改變。

站在用戶的角度思考問題,與客戶深入溝通,找到呼和浩特網(wǎng)站設計與呼和浩特網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、虛擬主機、企業(yè)郵箱。業(yè)務覆蓋呼和浩特地區(qū)。
一般來說方法有2種,一種就是客戶端用JS異步定時輪詢服務器端,這種是大部分人采用的方法,但是我在想到底可以不可以實現(xiàn)服務器端有改變時才會主動推送到客戶端呢?按理來說這種方式對于B/S是不可能實現(xiàn)的,因為B/S是無連接的,這種request/response的方式無法保持狀態(tài)。搜了一下,看到博客園有位朋友寫了篇文章是關(guān)于Server Push的,文章點這里看,我自己把它的代碼復制過來看了下效果,完整代碼如下:
前臺:
﹤%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ServerPushDemo.aspx.cs" Inherits="WebDemo.ServerPush.ServerPushDemo" %﹥
﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥
4
﹤html xmlns="http://www.w3.org/1999/xhtml" ﹥
﹤head runat="server"﹥
﹤title﹥無標題頁﹤/title﹥
﹤script type="text/javascript"﹥
function SetValue(time)
{
document.getElementById("TbTime").value=time;
}
﹤/script﹥
﹤/head﹥
﹤body﹥
﹤form id="form1" runat="server"﹥
﹤div﹥
﹤input type="text" id="TbTime"/﹥
﹤/div﹥
﹤/form﹥
﹤/body﹥
﹤/html﹥
后臺代碼:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
namespace WebDemo.ServerPush
{
public partial class ServerPushDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
Response.Buffer = true;
bool isOutput = false;
Response.Write("");
Response.Flush();
int lastsecond = 0;
while (Response.IsClientConnected)
{
Thread.Sleep(300);
if (DateTime.Now.Second != lastsecond && !isOutput)
{
lastsecond = DateTime.Now.Second;
Response.Write(" ");
Response.Flush();
isOutput = true;
}
else
{
isOutput = false;
}
}
}
}
}
這種方式的實現(xiàn)原理其實就是在OnRender事件里,用循環(huán)掛起連接,因為服務器端沒有response完畢,所以一直和客戶端保持連接,雖然這種方式能實現(xiàn)服務器端向客戶端的推送,但它的代價太大,因為web的優(yōu)勢就是無連接。這樣每個客戶端都要占用服務器端的一個IIS連接,如果用戶超過百個,就會非??植赖?。所以這種方式我覺得不可取,相比來說,還是用AJAX的方式定時輪詢更好。
真正意義上的Server Push好像我找了一些文章在Cgi里可以實現(xiàn)。但在.NET里的實現(xiàn)我就不得而知了,知道的高人請指點下,呵呵。
當前文章:淺析.NET中的Serverpush技術(shù)
網(wǎng)頁網(wǎng)址:http://m.5511xx.com/article/cocsogs.html


咨詢
建站咨詢
