Java EE 7 SDK |
This sample application is a Tic-tac-toe game that demonstrates how to use the Java API for WebSocket.
This sample application is a Tic-tac-toe game that demonstrates how to use the basic features of the Java API for Websocket.
The application consists of a client and a server, and it uses the client API and the server API from JSR-356.
The client is built with JavaFx, and it is located in the
org.glassfish.samples.tictactoe.client
package. The server is a WebSocket endpoint as defined in JSR-356, and it
is located in the org.glassfish.samples.tictactoe.server
package.
In this sample, two client instances create WebSocket connections to communicate with the server endpoint, which acts
like a hub. The server endpoint handles the interactions between the two clients. Once there is a winner, the clients
close the connection, and the onClose()
method of the server endpoint is invoked.
This sample shows how to use the WebSocket Client API in Java and how to create a WebSocket connection from a standalone client application and to server endpoint.
Note: Unlike the rest of the WebSocket samples, this sample does not use JavaScript code in an HTML page to connect to a server endpoint.
This sample demonstrates the following key features:
How to obtain a WebSocket container instance and how to connect to and endpoint from a Java client.
The following code is from TicTacToeClient.java
:
private void startGame() throws URISyntaxException, DeploymentException, IOException { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); container.connectToServer(LocalEndpoint.class, null, new URI(SERVER)); }
How to implement a client endpoint in a Java client.
The following code is from LocalEndpoint.java
:
@Override public void onOpen(Session session, EndpointConfig config) { ... } @Override public void onMessage(final String message) { ... }
The client endpoint class overrides two methods: onOpen
and onMessage
.
The onOpen
method is invoked when the WebSocket connection is established.
The onMessage
method is invoked when the client endpoints receives a message.
These two methods serve the same purpose as their equivalents in the JavaScript WebSocket API.
How to implement a server endpoint in Java EE. The server endpoint uses WebSocket annotations as demonstrated in the other WebSocket sample applications.
Perform the following steps to build, deploy, and run the application:
app_dir
is the sample application base directory: samples_install_dir/javaee7/websocket/tictactoe
.run
outcome.
cd server
mvn clean verify cargo:run
The following commands are going to install the JavaFx library into local maven repository:
mvn install:install-file -Dfile=$JAVA_HOME/jre/lib/jfxrt.jar -DgroupId=com.oracle -DartifactId=javafx -Dversion=2.2 -Dpackaging=jar
Note: $JAVA_HOME
is point to JDK repository
cd app_dir/client
mvn clean install
Note: You get two 3*3 chessboard dialogs if the sample runs successfully.
Follow the instructions on the user interface to play Tic-tac-toe.
If you get any exceptions from JavaFX, ensure that the client
pom.xml
contains the correct PATH to jfxrt.jar
.
clean
outcome to undeploy the sample application and to remove the temporary directories such as build
and dist
.
mvn clean
If you have problems when running the application, refer to the troubleshooting document.
Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.