Overview
Sample of using a WebSocket client within a JavaFX client application.
The project makes use of the Tyrus WebSocket framework.
Description
Creates a scene where the user can input their name, then submit
a request to a WebSocket server, which will respond with Hello <name>
,
the output of which is recorded in a label on the scene.
As this is built to be a self-contained demonstration application, a local WebSocket server is started when the application starts up and the local server is shutdown when the application is stopped.
Implementation Notes
-
Websocket communication occurs within an asynchronously executed JavaFX Task.
-
WebSocket endpoints are defined as annotated endpoints as documented in the specification for JSR-356: Java API for WebSockets.
-
A new server connection is created for each communication task (i.e. connection resources are not shared or reused between tasks).
-
This implementation is not proxy aware. For information on making the system proxy aware see the Tyrus proxy documentation.
-
For high traffic communication it would be recommended to use a different implementation which reuses such resources, for example:
- Using a Tyrus shared container OR
- Creating a persistent connection which is used for server calls (probably in conjunction with a the Tyrus client reconnect facility).
Sample Screenshot
Build Requirements
This project requires Oracle Java 8u20+ and Maven 3.2.3+.
Checkout
git clone https://github.com/jewelsea/javafx-websocket-test.git
Build
mvn com.zenjava:javafx-maven-plugin:8.1.2:web
Execution
To run the resultant application as a standalone jar:
java -jar target/jfx/app/javafx-websocket-test-jfx.jar
To run the resultant application as webstart app:
javaws target/jfx/web/javafx-websocket-test.jnlp
Attribution
This is a JavaFX Maven Plugin built project. Information on using the JavaFX Maven Plugin is at:
A skeleton for this project was generated using the JavaFX Maven Plugin Quickstart Archetype, then significantly modified after that.