To learn more about open source, please visit: 51CTO Open Source Basic Software Community https://ost..com PrefaceIf we want to implement functions like WeChat chat, it is obviously not enough to communicate within the network, so the soft bus does not work with such long-distance transmission. If we want to complete the WeChat chat function, the traditional method is to use webSocket to carry out full-duplex communication with the help of the server. What is WebSocket?WebSocket is a network communication protocol that performs full-duplex communication over a single TCP connection. Before webSocket, everyone used HTTP protocol for network communication. However, HTTP protocol is a stateless, connectionless, one-way application layer protocol. Therefore, the client can only make one-way requests to the server. The server cannot actively send messages to the client, which makes it difficult to carry out services such as real-time chat. Some developers use HTTP for long polling, which means that HTTP must keep connecting requests for a period of time to obtain the latest server messages. This is obviously inefficient and wastes resources. Therefore, WebSocket was born. Only one connection is required to maintain full-duplex communication. DemoNext, we will use the official WebSocket interface to implement a simple real-time chat room demo. The effect is as follows: Code ImplementationYou can see the interface description in the official documentation. We only need to use a few simple interfaces to meet our business needs. https://developer.harmonyos.com/cn/docs/documentation/doc-guides/websocket-connection-0000001333321005. 1. Apply for network permissionsRegister the network permission in the config.json file, which allows the program to open the network socket and make a network connection. "reqPermissions" : [ 2. Import the webSocket moduleimport webSocket from '@ohos.net.webSocket' ; 3. Create a webSocket objectThen we call the createWebSocket() interface to generate a webSocket object and save it. let ws = webSocket . createWebSocket (); 4. Connecting to the webSocket channelCall the connect() interface to connect. A URL is required as a parameter. In this demo, a previously developed server interface is used directly for calling, but it is not open to the public. Therefore, you only need to put the developed interface address into "wsURL". onInit () { 5. Subscribe to channel message updatesHere we call the on( type:'message' ) interface to listen for messages. It should be noted that the server passes a string type, so if the message is a JSON object, you need to use JSON.parse() to parse it and restore it to a JSON object. onInit () { 6. Send MessageThen call the send() interface to send the message. Note that if you want to pass a JSON object, you must use JSON.stringify() to serialize it to ensure that we pass it in the form of a stream string. In the callback of this interface, we can also print it out to see whether the message was sent successfully. sendMessage () { 7. Hide the title barCareful friends will find that the black title bar shown in my demo is gone. In fact, it can be hidden. Just add a few lines of code under module.abilities in the config.json file. "metaData" : { 8. Style designThe next step is to simply design the interface style and render the obtained messages. < div class = "container" > .container { You can click the following link to download the attachments related to the article: https://ost.com/resource/2210. To learn more about open source, please visit: 51CTO Open Source Basic Software Community https://ost..com. |
<<: Enterprises need to have six capabilities to achieve digital transformation
At the MWC that just ended last week, 5G can be s...
Recently, the National Standardization Administra...
Since Jon Postel defined IPv4 in RFC791 in 1981, ...
Technology is constantly evolving, and it's e...
The Internet we are familiar with used to mainly ...
On April 8, 2021, the NGINX Official Authorized C...
[[435282]] Chifeng City's e-government extran...
Today's article is the opening. In fact, I ha...
At present, 5G has become an important engine for...
DogYun (狗云) released a discount plan for the May ...
Nowadays, in a large living environment, there ar...
[[412862]] Image from Baotu.com Today I will disc...
It is no exaggeration to say that today's Int...
On November 25-26, 2016, the WOT 2016 Big Data Te...
Enterprise IT leaders have heard a lot about edge...