Java EE HTML5 WebSockets with numerous clients example
Te the previous tutorial – Java EE HTML5 WebSocket example – wij have implemented a ordinary Java EE WebSockets enabled application where the client sent a plain message to the server overheen a websocket and then received a sequence of responses from the server overheen the same channel.
Ter this tutorial wij will implement an application where clients will interactively send messages to each other through the server. This way wij will have an application where clients are te fact publishing and receiving events to/from the server overheen HTML5 websockets.
This tutorial considers the following environment:
WebSocket server endpoint
Let’s define a Java EE websocket server endpoint:
Similarly to what wij have done te the previous tutorial – Java EE HTML5 WebSocket example – wij are defining a ServerEndpoint that will be – spil the name states – the server endpoint listening from client websocket connections.
Wij have a static Schrijfmap that will hold all connected client sessions. This way wij may broadcast received messages to other clients (method onMessage). Note that when wij receive a message wij broadcast it to all connected clients except the one who originated the message ter the very first place.
Methods onOpen and onClose are called when a client connects or disconnects from the server endpoint respectively. So wij add or liquidate them from the sessions Opbergmap accordingly.
Note that when the parameter of type Session is included te the annotated ServerEndpoint methods the container will inject the session that corresponds to the client which originated the event.
Now wij need to write the client-side of our websocket test application:
This pagina contains a ordinary input text where a user may write a message and a button to publish the message to the server overheen the established websocket channel.
Every time wij receive a message from the server wij write it into the pagina.
For more information about the websocket connection definition and the respective events please refer to Java EE HTML5 WebSocket example.
Wij may now test our application by accessing the testing pagina from two distinct web browser instances:
Wij will see the Connection established message spil expected:
Now wij write a elementary message (ex: “hello”) ter one of the clients and click the “Broadcast message” button:
The other connected client will receive the published message. Note that the message will be published to every connected client except the one who published the message te the very first place (spil wij defined ter the server endpoint):
Wij have developed a sample application where clients use websockets te total extent to communicate with a Java EE application server, ie. publishing and receiving events.
Te our case the received events are sent by other clients. One can lightly use a similar treatment to enable periodic message sending from server to clients ter order to notify them of some arbitrary event occurrence.
The example source code is available for download at the end of this pagina. The test wasgoed executed te Glassfish Four (you will need a Java EE 7 compliant application server).