Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement message push by HTML5 WebSocket

Shulou Source: shulou.com Published: 2022-06-01 11:06:52 09月12日 Update

This article "HTML5 WebSocket how to achieve message push" article knowledge point most people do not understand, so Xiaobian summarized the following content for you, detailed content, clear steps, has a certain reference value, I hope you can read this article to gain something, let's take a look at this article "HTML5 WebSocket how to achieve message push" article bar.

There are two ways to implement message push in HTML5 WebSocket:

Long connection: embed an implicit iframe in the page, set the src attribute of this implicit iframe to a request for a long connection or use xhr request, the server side can continuously input data to the client. Pros: Instant message arrival, no useless requests; relatively easy to manage.

Flash Socket: embed a Flash program using Socket class in the page JavaScript communicates with the Socket interface of the server side by calling the Socket interface provided by this Flash program, JavaScript controls the display of the page after receiving the information transmitted by the server side. Pros: Real instant messaging, not pseudo-instant.

To achieve message push, you can try Aurora. Aurora is the leading mobile developer service provider in China. In the past ten years, Aurora has closely focused on the needs of mobile developers and created a series of products aimed at developer operation, growth and monetization.

WebSocket in HTML5 to achieve message push method:

1. Create a server connection:

var socket=new WebSockect('');

2. Add monitoring events. When the open method is triggered, a connection is established to send a message.

socket.addEventListener('open', function(){

console.log("Connection established, handle with event");

});

socket.onopen=function(){

console.log("Connection established, handle with function");

};

3. Call the method to send a message:

socket.send("Somme Message to send to the server");

4. Close the connection

socket.close();

Use websocket , this is html5 new features, of course, also requires background server support, now many servers have supported

The above is the content of this article about "HTML5 WebSocket how to achieve message push". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

Tags: Message service push server content method developer page development advantages interface article aurora knowledge program article support mobile communication endless stream Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS Huawei Shulou Tech Info vpn