Java EE 7 SDK 

Samples Main Page

The Tic-tac-toe WebSocket Sample Application

This sample application is a Tic-tac-toe game that demonstrates how to use the Java API for WebSocket.

Description

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.

Key Features

This sample demonstrates the following key features:

Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:

  1. Set up your build environment and configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee7/websocket/tictactoe.
  3. Build, deploy, and run the server application using the run outcome.

    cd server

    mvn clean verify cargo:run

  4. Before running the client application, you should make sure JavaFx library has been installed into local maven repository.

    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

  5. Run the client standalone application. You need two client endpoints. To obtain two client instances, type the following commands in two different terminal windows:

    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.

  6. Use the clean outcome to undeploy the sample application and to remove the temporary directories such as build and dist.

    mvn clean

Note: In NetBeans IDE, open the sample application projects (client and server) and use the IDE to build and run them.

Troubleshooting

If you have problems when running the application, refer to the troubleshooting document.



Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.