Java uses websocket and obtains httpsession source code analysis (recommended)

1: Scope of use of this article

This article is not limited to spring boot. Ordinary spring projects and even servlet projects are the same, but the methods of configuring some listeners are different.

After the author's practice, this paper confirms the perfect operation.

2: Spring boot uses websocket

2.1: dependent package

Websocket itself is a service provided by the servlet container, so it needs to run in the web container, like Tomcat we use. Of course, Tomcat has been embedded in spring boot.

Websocket follows the Java EE specification, so you need to introduce Java EE packages

Of course, in fact, Tomcat already comes with this package.

If you are in spring boot, you also need to add the starter of websocket

2.2: configure websocket

If it is not a spring boot project, such a configuration is not required, because if it is run in tomcat, Tomcat will scan the annotation with @ serverendpoint to become a websocket, and this bean needs to provide registration management in the spring boot project.

2.3: Java code of websocket

The core of using websocket is a series of websocket annotations, @ serverendpoint is registered and opened on the class.

In fact, I also feel very strange. Why not use interfaces to standardize. Even though some additional parameters can be defined in other attributes in the @ serverendpoint annotation, it is believed that they can be abstracted, but presumably JavaEE has its intention to do so.

2.4: browser side code

Browser side code requires the browser to support websocket. Of course, there are also sockets JS can support IE7, but I haven't used this. After all, ie is basically useless. Almost all browsers on the market support websocket.

So the connection is successful.

3: Get httpsession, source code analysis

Obtaining httpsession is a very necessary issue to discuss, because the Java background needs to know which user is currently, to process the user's business logic, or to authorize the user. However, because the protocol of websocket is different from that of HTTP, it is impossible to get the session directly. But the problem must always be solved, otherwise the scenario used by the websocket protocol will be gone.

3.1: get the tool class of httpsession, and analyze the source code in detail

Let's take a look at the source code of @ serverendpoint annotation

We see the last method, that is, the bold method. As you can see, it requires a serverendpointconfig Subclass of configurator, we write a class to inherit it.

When we override the modifyhandshake method, we can see three parameters. The last two parameters make us feel like we've seen them before. Let's check the source code of a handshakerequest

We found that it is an interface, which specifies such a method

There are corresponding comments above, which indicate that the corresponding httpsession can be obtained from the servlet API.

When we found this method, we were already relieved.

Then we can complete the unfinished code

In fact, through the above source code analysis, you should know the acquisition of httpsession. But here's another line of code

sec. getUserProperties(). put(HttpSession.class.getName(),httpSession);

What does this line of code mean?

Let's take a look at the declaration of serverenpointconfig

public interface ServerEndpointConfig extends EndpointConfig

We found that this interface inherits the endpointconfig interface. OK, let's take a look at the source code of endpointconfig:

We found such a method definition

Map
getUserProperties(); It can be seen that it is a map. It can also be understood from the method name that it is the storage of some attributes of the user. Since it provides the get method, it means that we can get the map and operate on the values in it,

So there's the one above

sec. getUserProperties(). put(HttpSession.class.getName(),httpSession);

So now, get the source code analysis of httpsession.

3.2: set the class of httpsession

As we said before, due to the difference between HTTP protocol and websocket protocol, it is impossible to obtain the protocol directly from websocket. Then in 3.1, we have written the code to obtain httpsession, but if it is released for execution, a null index exception will be reported, because this httpsession is not set.

OK, in this step, let's set httpsession. At this time, we need to write a listener.

Then we need to register this class as a listener. If it is an ordinary spring project or servlet project, it is either on the web XML, or use @ weblistener annotation.

Because this article is demonstrated by the spring boot project, only the code for configuring the listener by spring boot is written here. For other configuration methods, please Baidu yourself.

This is how @ bean annotations are used

Alternatively, you can use the @ weblistener annotation

Then use the @ servletcomponentscan annotation to configure the startup method.

3.3: get the user's session in websocket

Through the source code analysis just now, we know that there is a parameter in the @ serverendpoint annotation that can configure the class we inherited just now. OK, let's configure it now.

@ServerEndpoint(value = "/websocket",configurator = HttpSessionConfigurator.class)

Next, you can get the endpointconfig object in the method modified in the @ onopen annotation, and use this object to get the map we set previously

Now we get the user's session from the Java websocket.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips!

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
分享
二维码
< <上一篇
下一篇>>