Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Asp.net uses Comet to develop http persistent connection method tutorial

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "Asp.net uses Comet to develop http persistent connection method tutorial". In daily operation, I believe that many people have doubts about the method tutorial of Asp.net using Comet to develop http long connection. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "Asp.net using Comet to develop http long connection method tutorial". Next, please follow the editor to study!

Benefits: 1. Compared with AJAX polling, it saves resources and has less latency, 2. 5%. Compared with webSocket, it can be used in a wide range of scenarios.

1. First create an empty project for Asp.net MVC

Add a controller (the same code can also be used in Asp.net WebForm)

The copy code is as follows:

Public class CometController: Controller

{

Public ActionResult Test ()

{

Response.Buffer = false

While (true)

{

Response.Write (DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss FFF") + "|")

Thread.Sleep (500)

}

/ / you can't get here.

Return Content ("")

}

}

}

two。 Build another controller and View to display the HTML

The copy code is as follows:

Public class HomeController: Controller

{

/ /

/ / GET: / Home/

Public ActionResult Index ()

{

Return View ()

}

}

The code of View is more important

The copy code is as follows:

@ {

Layout = null

}

Index

Var req = false

Var lastDelimiterPosition =-1

$(document) .ready (function () {

GetData ()

});

Function getData () {

LoadXMLDoc ("/ Comet/Test")

}

/ / create a new XHR

Function createRequest () {

If (window.XMLHttpRequest & &! (window.ActiveXObject)) {

Try {

Req = new XMLHttpRequest ()

} catch (e) {

Req = false

} / / branch for IE/Windows ActiveX version

} else if (window.ActiveXObject) {

Try {req = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {

Try {

Req = new ActiveXObject ("Microsoft.XMLHTTP")

} catch (e) {

Req = false

}

}

}

}

/ / initiate a request

Function loadXMLDoc (url) {

Try {

If (req) {

Req.abort ()

Req = false

}

CreateRequest ()

If (req) {

Req.onreadystatechange = processReqChange

Req.open ("GET", url, true)

Req.send ("")

} else {

Alert ('unable to create request')

}

} catch (e) {alert (e.message);}

}

/ / check status

Function processReqChange () {

If (req.readyState = = 3) {

Try {

ProcessInput (req.responseText)

If (req.responseText.length > 3000) {

LastDelimiterPosition =-1; getData ()

}

}

Catch (e) {

Alert (e.message)

}

}

}

/ / split string

Function ProcessInput (input) {

Var text = input

Var nextDelimiter = text.indexOf ('|', lastDelimiterPosition + 1)

If (nextDelimiter! =-1) {

Var timeStamp = text.substring (nextDelimiter + 1)

If (timeStamp.length > 0) {

LastDelimiterPosition = nextDelimiter

ProcessTime (timeStamp)

}

}

}

/ / output or trigger any event

Function ProcessTime (time) {

Document.getElementById ('div1') [xss_clean] = time

}

3. The end result is:

A time is displayed on the page, updated every half a second

Of course, after you get the content, you can do whatever you want. Update DOM or execute js, (fortunately, there is a method of eval ~)

4. This example is just an implementation based on asynchronous Javascript

In fact, it can also be implemented with these two tags, especially the script tag can access and execute cross-domain javascript

At this point, the study on "Asp.net uses Comet to develop http persistent connection method tutorial" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report