Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of WebSocket in html5

Shulou Source: shulou.com Published: 2022-06-03 12:12:11 10月02日 Update

Editor to share with you what is the use of WebSocket in html5, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is WebSocket?

The server and client of WebSocket can communicate in both directions and allow cross-domain communication. Supported by HTTP/1.1 's Upgrade mechanism and communicating through ws (non-encrypted) or wss (encrypted) protocols

WebSocket WebSocket (in DOMString url, in optional DOMString protocols); WebSocket WebSocket (in DOMString url, in optional DOMString [] protocols); WebSocket in HTML5

HTML5 only focuses on API on the client side, while each language implements it on the server side.

/ / create an Socket instance var socket = new WebSocket ('ws://localhost:8080'); / / Open Socket socket.onopen = function (event) {/ / send an initialization message socket.send (' I am the client and I\'m listeningrequests'); / / listen for message socket.onmessage = function (event) {console.log ('Client received a message',event);} / / turn off monitoring Socket socket.onclose = function (event) {console.log ('Client notified socket has closed',event);}; / / close Socket.... / / socket.close ()}

Event

Onclose onerror onmessage onopen

Attribute

ReadyState: CONNECTING 0 OPEN 1 CLOSING 2 CLOSED 3

BinaryType: String Blob ArrayBuffer

Compatibility

Method 1:

If the client does not support WebSocket, there are several candidates for Flash Socket AJAX long-polling AJAX multipart streaming IFrame JSONP polling

Method 2

Using Socket.io to smooth out the differences, the library can automatically connect with a message push method supported by the browser when the browser does not support WebSocket. The library will also detect whether the connection is dropped and automatically reconnect for you when it is dropped.

/ / create a Socket.IO instance and establish a connection var socket= new io.Socket ('localhost', {port: 8080,}); socket.connect (); / add a connection listener socket.on (' connect',function () {console.log ('Client has connected to the server');}); / / add a connection listener socket.on ('message',function (data) {console.log (' Received a message from the server listener data);}) / / add a listener socket.on ('disconnect',function () {console.log (' The client has disconnect');}) that closes the connection; / / send a message via Socket to the server function sendMessageToServer (message) {socket.send (message);} advantage

Real-time two-way communication

Good native support for browsers (compatibility can be easily solved with third-party libraries)

Support for custom protocols

Practical application

Chatting room

Server message push

Front and rear real-time system

The above is all the content of this article "what is the use of WebSocket in html5?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Support messaging monitoring services clients servers browsers listeners articles communications browsing compatibility content bidirectional instances real-time clients methods encryption push Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Redmi Apple Shulou Technology