Using Binary WebSockets With Java EE 7 and JavaFX Clients
Using Binary WebSockets With Java EE 7 and JavaFX Clients
Overview
In this section, you create a Java EE 7 web application in the NetBeans IDE.
3. On the Name and Location page, enter BinaryWebSocketServeras the project name and click Next.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 1/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
4. Add a static Setof Sessionobjects just below the public class BinaryWebSocketServer {definition to store connected
private
static final Set<Session> sessions =
Collections.synchronizedSet(new HashSet<Session>());
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.Session;
Alternatively, you can import the classes by selecting the Fix Imports option.
2. In the Fix All Imports dialog box, select the classes that you want to import and click OK.
@OnOpen
public void onOpen(Session session) {
sessions.add(session);
}
@OnClose
public void onClose(Session session) {
sessions.remove(session);
}
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
@OnMessage
public void onMessage(ByteBuffer byteBuffer) {
for (Session session : sessions) {
try {
session.getBasicRemote().sendBinary(byteBuffer);
} catch (IOException ex) {
Logger.getLogger(BinaryWebSocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
package com.demo.websocket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/images")
public class BinaryWebSocketServer {
@OnOpen
public void onOpen(Session session) {
sessions.add(session);
}
@OnClose
public void onClose(Session session) {
sessions.remove(session);
}
@OnMessage
public void onMessage(ByteBuffer byteBuffer) {
for (Session session : sessions) {
try {
session.getBasicRemote().sendBinary(byteBuffer);
} catch (IOException ex) {
Logger.getLogger(BinaryWebSocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 4/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
3. On the Name and Location page, enter JavaFXBinaryWsClientfor the project name and click Finish.
To use WebSocket in Java SE/FX applications, you need additional JAR libraries. This example uses Project Tyrus, which is the ref
WebSockets. The required JAR files are provided as part of this tutorial.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 5/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
6. On the Name and Location page, enter libfor the folder name and click Finish.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 6/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
10. In the Add JAR/Folder dialog box, add the unzipped JAR files.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 7/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
If you see a reference warning about the dist.jarfile, ignore it. The file is built with the project
You will:
View Code
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 8/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
3. Add the following instance variables:
@ClientEndpoint
public class JavaFXBinaryWsClient extends Application {
import java.util.logging.Logger;
import javafx.scene.image.ImageView;
import javax.websocket.Session;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.WebSocketContainer;
@Override
public void start(final Stage primaryStage) {
connectToWebSocket();
7. Replace the contents of the startmethod after the connectToWebSocket()call to create the layout.
@Override
public void start(final Stage primaryStage) {
connectToWebSocket();
Button btn = new Button();
btn.setText("Send Image!");
btn.setPrefSize(400, 27);
AnchorPane.setTopAnchor(btn, 0.0);
AnchorPane.setLeftAnchor(btn, 0.0);
AnchorPane.setRightAnchor(btn, 0.0);
AnchorPane.setTopAnchor(imageView, 27.0);
AnchorPane.setBottomAnchor(imageView, 0.0);
AnchorPane.setLeftAnchor(imageView, 0.0);
AnchorPane.setRightAnchor(imageView, 0.0);
root.getChildren().add(btn);
root.getChildren().add(imageView);
primaryStage.setTitle("Image push!");
primaryStage.setScene(scene);
primaryStage.show();
}
javafx.scene.layout.AnchorPane;
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 9/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javafx.stage.FileChooser;
11. Create an event handler for the button and invoke the selectAndSendImagemethod. Make the stage method parameter in the start
handler.
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
selectAndSendImage(primaryStage);
}
});
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
@OnOpen
public void onOpen(Session session) {
this.session = session;
}
@OnMessage
public void onMessage(InputStream input) {
System.out.println("WebSocket message Received!");
Image image = new Image(input);
imageView.setImage(image);
}
@OnClose
public void onClose() {
connectToWebSocket();
}
import javafx.scene.image.Image;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 10/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
package javafxbinarywsclient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
@ClientEndpoint
public class JavaFXBinaryWsClient extends Application {
@OnOpen
public void onOpen(Session session) {
this.session = session;
}
@OnMessage
public void onMessage(InputStream input) {
System.out.println("WebSocket message Received!");
Image image = new Image(input);
imageView.setImage(image);
}
@OnClose
public void onClose() {
connectToWebSocket();
}
@Override
public void start(final Stage primaryStage) {
connectToWebSocket();
AnchorPane.setTopAnchor(btn, 0.0);
AnchorPane.setLeftAnchor(btn, 0.0);
AnchorPane.setRightAnchor(btn, 0.0);
AnchorPane.setTopAnchor(imageView, 27.0);
AnchorPane.setBottomAnchor(imageView, 0.0);
AnchorPane.setLeftAnchor(imageView, 0.0);
AnchorPane.setRightAnchor(imageView, 0.0);
root.getChildren().add(btn);
root.getChildren().add(imageView);
primaryStage.setTitle("Image push!");
primaryStage.setScene(scene);
primaryStage.show();
}
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 11/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
}
The http://localhost:8080/BinaryWebSocketServer URL opens in a web browser after GlassFish server starts.
3. Repeat step 2 to run another instance of the JavaFXBinaryWsClient project. (Do not close the window.)
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 12/14
6/29/2017 Using binary WebSockets with Java EE 7 and JavaFX clients.
4. In one of the windows, press the Send Image! button, browse for an image, and open it.
5. Open more instances of the JavaFX applications and send another image. All instances will receive and display the images.
Summary
Next Steps
This is a simple tutorial for binary WebSockets. You can try to implement more complex scenarios, such as the following, and use
Resources
To learn more about WebSockets or Java EE7, see the following resources:
Credits
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html 14/14