Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of im Real-time Communication based on websocket

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Based on websocket im real-time communication example analysis, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.

How to run after downloading the source code:

Operating environment: . NET Core 2.1 + redis-server 2.8

Download Redis-x64-2.8.2402.zip, click start.bat to run; or modify the imServer, web appsettings.json redis configuration, pointing to the available redis-server

cd imServer && dotnet run --urls="http://0.0.0.0:6001"

cd web && dotnet run --urls="http://0.0.0.0:5555"

Open multiple browsers, visit http://127.0.0.1:5555 Send group messages

Design idea socket selection

The second best solution is to use websocket on the browser side and socket on the other side. Such a chaotic design will eventually be very difficult to maintain.

Therefore, it is strongly recommended that all terminals use websocket protocol, adorid/ios/h6/Mini programs all support websocket client.

Service and Communication Protocol

IM systems generally involved [My Friends],[My Groups],[History Messages], and so on.

So, what relationship should imServer maintain with the business side (web)?

User A sends a message to friend B and analyzes it:

It is necessary to determine whether B is A's friend;

It is necessary to determine whether A has authority;

Wait a minute.

Such business decisions can be complex, and let's imagine if imServer is used for business protocols, it will become a giant and difficult to maintain.

And if you get history, does the client have to first websocket.send ('gethory ') and then locate callback processing in onmessage?

Do this two tenths...

We design this way, all users 'active behavior goes to the business side (web), imServer is only responsible for instant message push. What do you mean?

User A sends a message to friend B: the client requests a web interface, and the web backend initiates a push request to imServer. After receiving the instruction, imServer sends data to the websocket of front-end user B, and user B receives the message.

Get history message: client requests service side (web) interface and returns json(history message)

Receipt: How does User A know the status of message delivery (success or failure or not online)? imServer sends messages to user B, push the status to user A in the form of messages (according to the logic above), please see the source code for details.

web notifications imServer performance optimization

With message queues, redis 'publish-subscribe is the lightest.

Enable multi-node deployment

How many websocket connections does a single imServer instance support? A few hundred is no problem, OK...

What if the system has 10,000 online users?

You can deploy 4 imServers according to the hash partition of id:

imServer1 Subscribe to redisChanne1

imServer2 Subscribe to redisChanne2

imServer3 Subscribe to redisChanne3

imServer4 Subscribe to redisChanne4

The service side (web) locates the corresponding redisChannel according to the hash partition algorithm of the receiver ID, so that publish can locate the message to the corresponding imServer.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report