当前位置:网站首页>Summer Challenge [FFH] real time chat room websocket practice
Summer Challenge [FFH] real time chat room websocket practice
2022-07-27 17:47:00 【51CTO】
This article is participating in the starlight project 3.0– Summer Challenge
# Summer Challenge #【FFH】 Real time chat room WebSocket actual combat
Preface
If you want to realize the same function as wechat chat , It is obviously not enough to communicate within the network , Therefore, the soft bus does not work with this kind of long-distance transmission . If we want to complete the chat function of wechat , The traditional method is to use webSocket Full duplex communication with the help of server .
WebSocket What is it? ?
WebSocket yes One in a single TCP Network communication protocol for full duplex communication on the connection .
There was no such thing as webSocket When , We all use HTTP Protocol for network communication , however HTTP The protocol is a stateless , There is no connection , 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 , As a result, it is difficult for businesses such as real-time chat to carry out .
Developers can use HTTP The scheme of long polling , That is to say, we need HTTP The connection request must be maintained for a period of time , To get the latest server messages . This is obviously inefficient , And it's a waste of resources .
So it was born WebSocket, You only need to connect once , You can always maintain full duplex communication .
Demo Exhibition
Now let's use the official WebSocket Interface to achieve a simple real-time chat room Demo. The effect is as follows :
Code implementation
You can see the interface description in the official document , We only need a few interfaces to realize our business requirements .

① Apply for network permission
stay config.json Register network permissions in the file , This permission allows the program to open a network socket , Make a network connection .
② Import webSocket modular
③ establish webSocket object
Then we call createWebSocket() Interface generates a webSocket object , And save it .
④ Connect webSocket passageway
call connect() Interface . I need a URL Pass in as a parameter , In this demo in , Directly use a previously developed server interface to call , But not open to the outside world , Therefore, you only need to put the interface address you have developed into ”wsURL“ Inside you can .
⑤ Subscribe to message updates in the channel
Here we call on( type:‘message’ ) Interface for message listening , It should be noted here that the string type passed by the server , So if the message is JSON object , You need to use JSON.parse() To analyze , restore JSON object .
⑥ Send a message
Next call send() Interface to send messages , Note here , If you want to pass JSON object , To use JSON.stringify() Do serialization , Make sure we pass in the form of a stream string .
In the callback of this interface , We can also print it out , Check whether the message is sent successfully .
⑦ Hide the title bar
Careful friends will find , my demo The black title bar of the display is missing , In fact, it can be hidden , Only need config.json In file module.abilities Add a few lines of code below .
⑧ Pattern design
Next, simply design the interface style , Render the obtained message .
Attachment link : https://ost.51cto.com/resource/2210
Want to know more about open source , Please visit :
边栏推荐
- (2)融合cbam的two-stream项目搭建----数据准备
- 【单片机】2.3 AT89S52的CPU
- kubernetes 1.24高可用集群二进制部署
- Database hyperphone (II)
- 【obs】x264_encoder_encode 编码输出pts dts和 framesize
- C语言怎么学?这篇文章给你完整答案
- 一个端到端的基于 form 表单的文件上传程序,包含客户端和服务器端
- Understanding service governance in distributed development
- Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
- 信通院陈屹力:降本增效是云原生应用最大价值
猜你喜欢

Explain the idempotence of distributed system in detail

Because the employee set the password to "123456", amd stolen 450gb data?

KMP模板——字符串匹配

数据库超话(一)

MLX90640 红外热成像仪测温传感器模块开发笔记(七)

Today's sleep quality record 82 points

With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?

灵魂一问:为什么ES比MySQL更适合复杂条件搜索?

笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误

可口可乐的首要挑战,不是元气森林
随机推荐
Kubernetes Chapter 8: deploy NFS system with kubernetes to complete database persistence (kubernetes work practice class)
数据库超话(四)
Database hyperphone (4)
Lichuang EDA - layout and inspection of schematic diagram (III)
Chery omenda is also too similar to Chang'an uni-t, but does it look like it? Is the product power like it?
Explain the pile of binary trees in detail
如何开发一款在线Excel表格系统(上)
Switch and router technology-02-working principle of Ethernet switch
阿里巴巴鹰眼系统简介
科目三: 直线行驶
The 7-year-old boy broke his finger by AI robot just because he played chess too fast?
【数据库系统概论(王珊)】第4章——数据库安全性
Switch and router technology-03-basic configuration of switch
How to extract tables from PDF through C /vb.net
#yyds干货盘点# 面试必刷TOP101:链表内指定区间反转
格力「不清凉」:巨头诉讼落幕又遭大经销商减持,空调新战场还晚人一步?
Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
Cow! His secret is to reproduce the paper in 2 hours——
numpy数组矩阵操作
Windows and network foundation-15-local security policy