In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use Websocket in h5. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
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
This is the end of this article on "how to use Websocket in h5". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.