Spring boot + layim + t-io example code for file upload and monitoring user status

preface

Today's main content is: pictures in layim messages, uploading and docking of files, monitoring of user status and monitoring of group online number. Now I will introduce them one by one.

Picture upload

There are many blogs about file upload in spring boot, and I also excerpted some of the code. The upload part briefly introduces the problems encountered in the development process. First, let's take a look at the corresponding interfaces of layim:

Yes, that's right. We only need to write two interfaces to realize the function of sending pictures and files in layim. I save some trouble here. Because layim has judged the file type, I only use one upload interface here. The returned format is maozi:

Uploading the code is very simple. Get the file suffix, generate the name of the guid, and save it to the corresponding folder. Finally, return the response data that layim wants.

At first, I wanted to directly transfer it to the / resources / static / upload / folder. Later, I found that 404 would appear in the access path after the upload was successful. The reason is that even if there is a file in the folder, there is no file in the corresponding folder of the target, so it can only be recompiled and run. That's certainly not possible. So I found a solution. Later, I saw such a processing method, that is, map each file request to a path in the controller, and then use resourceloader to find the response file. The code is as follows:

Get the file code. When accessing that file, specify the file name, and the resource will return the corresponding resource according to the path.

Take a look at the effect: (in order to show you the problem of uploading file size limit when making demo screenshots, just change the configuration.)

The code value in the above is configured as: filename:. +, In this way, if we encounter an access path, such as / ABC Jpg will match the GetFile method. Then the resourceloader helps us find the file. But to learn more, I tracked the source code. Let's first look at the getresource method in defaultresourceloader.

Later, during debugging, I found that, for example, the path I visited was / ABC Gif, then it will be converted to this address in the background: file:///G:/JavaProject/SpringBootLayIM/upload/abc.gif In the same way, opening this address directly on the local machine can also be accessed. Address above( file:///.. )It's layim upload. The value of dir. Similarly, changing the folder to resources / static / upload / also applies, because through resourceloader, it locates the file to the physical address of the server.

There is not even the uplood folder under target, but the files can still be accessed

File upload

It uses the same interface as the picture. The difference is that the configuration needs to be changed to upload large files. It has been written above and will not be detailed here.

User online status

In the status monitoring of layim, there is such an item:

In fact, the user's online and offline status should be loaded when reading the list. However, this is just a demonstration. When the window is switched, check the status of the opposite side. Therefore, after the chatchange event is triggered, we send a request to the server.

There are two situations above. The first is when I talk alone, I need to know each other's status. Second, I want to know how many people are online during group chat. So I made a slight distinction.

Two new message processing classes are added:

In fact, the code to judge whether it is online is as follows:

The encapsulated message is then returned to the server. The message flow has been introduced in the implementation of single chat and group chat, which will not be repeated here.

How to judge the number of people in the group is as follows:

Look at the effect. The amount of data is a little small, just two users, but it's enough for demonstration.

After logging in with another account:

summary

The content of this article is not very much, just a file upload and a simple application of individual APIs in TiO, but what excites me is that when I debug the code, I found a lot of playable things, such as this:

Of course, the author has introduced it in the introduction of the framework. You can listen to the messages sent by each client as statistics. Therefore, we can do something with these data in the next step.

Finally, since the rest of the content is some simple message history, message saving, friend application and other additions, deletions, modifications and queries, I won't introduce them too much in the follow-up. Send an article after the project is officially written. Next, I'm going to play with the data in t-io ~ ~ (wait, is it deviated? I was learning spring boot...)

Code address: https://github.com/fanpan26/SpringBootLayIM

summary

The above is the example code of spring boot + layim + t-io to upload files and monitor user status introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>