package geniusweb.examples; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketFactory; import geniusweb.profile.Profile; /** * This example shows how a client can download a profile from the * profileserver. It assumes that the server is up and running on * localhost:8080/profilesserver. */ public class DownloadProfileExample2 extends WebSocketAdapter { private String received = ""; public DownloadProfileExample2() throws WebSocketException, InterruptedException, IOException { WebSocketFactory factory = new WebSocketFactory() .setConnectionTimeout(5000); WebSocket ws = factory.createSocket( "ws://localhost:8080/profilesserver/websocket/get/jobs/jobs1"); ws.addListener(this); ws.connect(); Thread.sleep(2000); // just briefly wait for possible updates ws.disconnect(); } public String getReceived() { return received; } /** * We receive new profiles when they are changed; or null if the profile was * removed. */ @Override public void onTextMessage(WebSocket websocket, String json) throws Exception { received = json; System.out.println("received profile: " + new ObjectMapper().readValue(json, Profile.class)); } public static void main(String[] args) throws WebSocketException, InterruptedException, IOException { new DownloadProfileExample(); } }