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

How to send messages directly to the server in html

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to send messages directly to the server in html. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The HTML5 server sends an event (Server-Sent Events) that allows a web page to get updates from the server.

Sever-Sent event-single message delivery

The Sever-Sent event means that the web page automatically gets updates from the server.

It was possible to do this in the past, provided that the web page had to ask if there were any updates available, and the updates could arrive automatically through the time sent by the server.

Examples: FaceBook/Twitter updates, valuation updates, new blog posts, competition results, etc.

Accept Sever-Sent event notifications

The EventSource object is used to receive notification of events sent by the server: get server update data from the runoob.com

Instance resolution:

1. Create a new EventSource object, and then specify the page URL to send updates (in this case, "demo.sse.php")

two。 If an update is not received, an onmessage event will occur

3. When the onmessage event occurs, push the accepted data into the element whose id is "result"

Detect Server-Sent event support

As an example, an additional piece of code is written to check the browser support for events sent by the server if? (type (EventSource)?! =? "undefined")

{

/ / browser support? Server-Sent

/ / some code

/ / the browser does not support it? Server-Sent

}

Server code example

In the future, for the above examples to work, we also need servers that can send data updates (such as PHP and ASP)

The syntax of the server-side event flow is very simple, setting the Content-Type header to text/event-stream. Now, you can start sending the event stream.

Example: ASP? Code? (VB)? (demo_sse.asp):

Code interpretation:

1. Set the header "Content-Type" to "text/event-stream"

two。 Stipulate that pages are not cached

3. Output date sent (always starts with "data:")

4. Refresh output data to a web page

EventSource object

In the above example, we use onmessage time to get the message, but we can also use other events

Event description

Onopen when the connection to the server is opened

Onmessage when a message is received

Onerror when an error occurs

This is the end of the article on "how to send messages directly to the server in html". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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