This contribution may be useful for any one who likes tho implement a simple WebSocket Server/Client for the use with Apache Tomcat for education purposes or further development.
Implements server/client communication over the WebSocket protocol for the integration into the Apache Tomcat Web Application Framework. The following features are demonstrated (full-duplex,bidirectional):
- Message created in one session seen on all other sessions
- Message removal on all sessions
- Message propagated or supressed on specific sessions
This is an adaption from the Oracle tutorial with the following modifications:
- Edited for deployment on Apache Tomcat (instead of Glassfish)
- Generalized message model
- Message propagation for specific session
Reference: Java EE 7: Building Web Applications with WebSocket, JavaScript and HTML5 https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html
The source code includes
- HTML, JS, CSS for the rendering and handling of the messages on the client side (browser)
- Server side implementation for the WebSocket call backs
- Session management and message handling triggered by the call backs
Printscreen of two browsers (sessions) seeing the same messages simultaneously, and one message seen only on the session which declared it private.
Set up the project in your environment:
- Install Apache Tomcat
- Add a new Java Web Jakarta EE 11 project in your IDE
- Change the context path /402_NRAS_WS suitable for your environment/project
- Pull or download the files from this repository
- Change the context path in the websocket.js WebSocket initialization call
- Dowload the declared dependencies (see pom.xml), JSON jar must be downloaded manually in some cases
Niklaus Aeschbacher (com.nras.*)