Preface
This Monday, I need to hand in the android big experiment homework. The android big experiment has made a second-hand mall, but the functions are too few. The android teacher requires the big experiment to have some characteristics. Just thinking about real-time chat. Then I tried it out on Sunday.
socket
Socket connection is required for real-time chat. Ordinary background requests are http or https requests, which means that the foreground sends a request and the background responds to the request, and it ends. The socket protocol is a long connection. Once a connection is established in the foreground or background, the connection can only be disconnected after one party issues a close. In the connection, the client and the server can send information to each other at will. This is very suitable for real-time chat situations. If the real-time chat uses the commonly used http protocol, not only will a request be sent every time a message is sent, this will take up a lot of server resources, and the server cannot actively send messages to the receiving client.
Interface implementation
The chat interface is essentially a list, as long as you use the list layout commonly used in android, you only need to determine whether a message is sent by yourself. If it is sent by yourself, you need to display the information
The specific implementation can be achieved by finding a blog on the Internet. It is worth noting that each chat bubble is still very interesting. The chat bubble we see is also a background image, but it is a background image in a special format. It will stretch as the content changes, all of which are android If you change yourself, the official calls this kind of picture a point nine pictures. Let’s take a look at the difference between using point nine maps and not using point nine maps.
The dot nine pictures can also be made by yourself, here we understand his general principle
The upper black line represents the stretched area during horizontal stretch,
The black line on the left represents the vertically stretched area,
The black line at the bottom and the black line on the right represent the display area of the content,
The display effect is shown in the figure.
The point nine picture needs to be set as a background picture and has a name ending in .9. Such as background.9.png.
Maybe because there are not many scenarios in use, and there are no ready-made resources on the Internet, I went to a finished product project on github and found one and used it directly.
Give everyone a connection to this project, no need to find https://github.com/zhaokaiqiangZzz/deal-box/tree/main/dealbox/app/src/main/res/drawable
achieve
The realization of interactive communication refers to another blog. https://xlui.me/t/spring-boot-websocket-android-client/
The general principle is that two clients establish socket communication with the server respectively, and subscribe to the message of an address at the same time. Of course, this address has this user id. When sending a message, it needs to be sent to another address with the recipient's id. The server is responsible for receiving the sent data and sending the received data to the recipient through the recipient's id.
For specific implementation methods, please refer to the blog above. The blog implements broadcasting, dynamic groups, and peer-to-peer messaging. You can also see this project
https://github.com/zhaokaiqiangZzz/deal-box only uses point-to-point messaging.
Troublesome place
There is a problem. When designing the database, chat and user have a many-to-many relationship, but there are actually only two users in the chat, corresponding to the two people A and B who are chatting. For A, B is his chat partner, and for B, A is his chat partner. It is necessary to judge whether chat.users.get(0) and the currently logged-in user id are equal to display the user name and avatar of the other party and oneself.
I haven't thought of a good solution for the time being.
to sum up
In the process of realization, many compromises were made due to the tight time. But to write a big experiment, as long as it is realized, the teacher does not care whether there will be bugs or what realization you used. Thank you Huang Tingxiang for your help in my realization
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。