当前位置:网站首页>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 :
边栏推荐
- Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
- 【数据库系统概论(王珊)】第11章 并发控制
- Uncle's nephew and his students
- Hegong sky team vision training Day8 - vision, target recognition
- 数据库超话(四)
- MySQL: 函数
- Motion capture system for end positioning control of flexible manipulator
- Establishing SSL connection without server‘s identity verification is not recommended
- 运行loam_velodyne实时建图
- Branch loop statement of C language
猜你喜欢

Understanding service governance in distributed development

交换机和路由器技术-02-以太网交换机工作原理

选择体育场馆的LED显示屏时应该注重哪些方面

成本高、落地难、见效慢,开源安全怎么办?

Kubernetes Part 7: using kubernetes to deploy prometheus+grafana monitoring system (kubernetes work practice class)

Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader

Neural network implementation of handwritten numeral classification matlab

快解析结合海典医药

信号量保护之位带操作

DDD(领域驱动设计)分层架构
随机推荐
数据库超话(一)
Redis: 配置AOF不起作用
写好技术原创文章的一点建议
Establishing SSL connection without server‘s identity verification is not recommended
Motion capture system for end positioning control of flexible manipulator
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
腾讯云上传使用
KMP template - string matching
Coca Cola's primary challenge is not vitality forest
Mysql database defines cursor in trigger
密集光流提取dense_flow理解
Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
7 岁男孩被 AI 机器人折断手指,仅因下棋太快?
Database hyperphone (III)
Functions of C language
MySQL: 函数
美团到餐“祖传数仓”标准化治理笔记
详解分布式系统的幂等
树莓派驱动代码的编译和测试
wallys/DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power.