package geniusweb.examples; import java.io.IOException; import java.net.URI; import javax.websocket.ClientEndpoint; import javax.websocket.ContainerProvider; import javax.websocket.DeploymentException; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.WebSocketContainer; /** * This example shows how a client can download a profile from the * profileserver. It uses the annotation-based javax.websocket mechanism. It * assumes that the server is up and running on localhost:8080/profilesserver. * We're a bit lazy here and give this example class also the annothation * "@ClientEndpoint" to avoid having to create a separate class containing the * actual endpoint. */ @ClientEndpoint public class DownloadProfileExample { // this must be static because connectToServer will create new instance. private static String received = ""; public DownloadProfileExample() { } @OnOpen public void onOpen(Session session) { System.out.println("Connected to endpoint: " + session); } @OnMessage public void processMessage(String message) { System.out.println("Received message in client: " + message); received = message; } @OnError public void processError(Throwable t) { t.printStackTrace(); } public void run() throws InterruptedException, IOException { try { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); String uri = "ws://localhost:8080/profilesserver/websocket/get/jobs/jobs1"; System.out.println("Connecting to " + uri); container.connectToServer(DownloadProfileExample.class, URI.create(uri)); Thread.sleep(2000); } catch (DeploymentException | IOException ex) { ex.printStackTrace(); } } public String getReceived() { return received; } }