Javaee7 + WebSockets + glassfish4 create a chat room

A single two-way connection is established between the client and the server, which means that the client only needs to send a request to the server, and the server will process it and return it to the client. The client can continue to do other work at this time. The whole process is asynchronous. In this series of tutorials, users will be guided how to use the new parsing JSON API (jsr-353) in Java EE 7 in GlassFish 4, the container of Java EE 7, and how to comprehensively use jQuery and bootstrap. This article requires readers to have some basic knowledge of HTML 5 websocket.

design sketch

Let's take a look at the rendering after completing this tutorial, as follows:

preparation

We use JDK 7 and mavn 3 to build the library. First, look at POM Section about Jave EE 7 in XML:

Meanwhile, in order to use GlassFish 4, the following plug-ins need to be added:

Set the endpoint of websocket

Let's first look at the code of the server websocket as follows, and then make further analysis:

Let's analyze the above code:

Use @ serverendpoint to define a new endpoint, where the value specifies the URL and the pathparams parameter can be used, just as it is used in jax-rs.

Therefore, the value "/ chat / {room}" allows users to connect to a chat room through the following URL: WS: / / 0.0 0.0:8080/hascode/chat/java

The value in braces (i.e. room) can be injected as a parameter in the lifecycle callback method of the endpoint by using javax.websocket.server.pathparam.

In addition, we need to use an encoding and decoding class, because we use a dto class to transfer data between the server and the client.

When the user connects to the server for the first time and inputs the room number to enter the chat room, the room number will be injected and submitted in the form of parameters, and session Getuserproperties saves the value in the user's property map.

When a chat participant sends information to the server through a TCP connection, it loops through all open sessions. Each session is bound to the specified chat room and receives encoded and decoded information.

If we want to send simple text messages or messages in binary format, we can use session getBasicRemote(). Sendbinary() or session getBasicRemote(). sendText()

Next, let's look at the code used to represent the dto (data transfer object), as follows:

Conversion of chat messages

In this application, an encoding and decoding class will be written to convert chat information to JSON format.

Let's first look at the implementation of the decoding class, which will convert the chat information delivered to the server into the chatmessage entity class. Here, the Java API for JSON processing (jsr353) specification is used to convert JSON format information into entity classes. The code is as follows. The overridden willdecode method returns true by default.

Similarly, look at the code of the encoding class. Instead, this class converts the chatmessage class to JSON format. The code is as follows:

You can see the power of jsr-353 here. You only need to call JSON Createobjectbuilder can easily convert a dto object into JSON.

Build a simple client through bootstrap and javacsrip

Finally, we use the famous bootstrap, jQuery framework and JavaScript to design a simple client. We'll create a new index in the Src / main / web app directory HTML file, the code is as follows:

In the above code, pay attention to the following points:

If you want to call websocket on the JavaScript side, you need to initiate a connection in the following way: WS: / / IP: port / context_ PATH/ENDPOINT_ URL e.g ws://0.0. 0.0:8080/hascode/chat/java

The method of creating a websocket connection is very simple, using VAR wsocket = new websocket ('WS: / / 0.0.0.0:8080 / hascode / chat / Java ');

To get the information returned from the server, you only need to use the callback function wsocket Set the corresponding method to get the returned information in onmessage.

Send a websocket message to the server using wsocket Send(), where the message that can be sent can be text or binary data.

Wsocket. Is used to close the connection close()。

Finally, we deploy the code through MVN package embedded GlassFish: run, and then we can see the effect of the screenshot at the beginning of this article.

The above is the chat room implemented with javaee7, WebSockets and glassfish4. I hope it will be helpful to your study.

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