[25] | 1 | package geniusweb.examples;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 |
|
---|
| 5 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 6 | import com.neovisionaries.ws.client.WebSocket;
|
---|
| 7 | import com.neovisionaries.ws.client.WebSocketAdapter;
|
---|
| 8 | import com.neovisionaries.ws.client.WebSocketException;
|
---|
| 9 | import com.neovisionaries.ws.client.WebSocketFactory;
|
---|
| 10 |
|
---|
| 11 | import geniusweb.profile.Profile;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * This example shows how a client can download a profile from the
|
---|
| 15 | * profileserver. It assumes that the server is up and running on
|
---|
| 16 | * localhost:8080/profilesserver.
|
---|
| 17 | */
|
---|
| 18 | public class DownloadProfileExample2 extends WebSocketAdapter {
|
---|
| 19 |
|
---|
| 20 | private String received = "";
|
---|
| 21 |
|
---|
| 22 | public DownloadProfileExample2()
|
---|
| 23 | throws WebSocketException, InterruptedException, IOException {
|
---|
| 24 | WebSocketFactory factory = new WebSocketFactory()
|
---|
| 25 | .setConnectionTimeout(5000);
|
---|
| 26 | WebSocket ws = factory.createSocket(
|
---|
| 27 | "ws://localhost:8080/profilesserver/websocket/get/jobs/jobs1");
|
---|
| 28 | ws.addListener(this);
|
---|
| 29 | ws.connect();
|
---|
| 30 | Thread.sleep(2000); // just briefly wait for possible updates
|
---|
| 31 | ws.disconnect();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public String getReceived() {
|
---|
| 35 | return received;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * We receive new profiles when they are changed; or null if the profile was
|
---|
| 40 | * removed.
|
---|
| 41 | */
|
---|
| 42 | @Override
|
---|
| 43 | public void onTextMessage(WebSocket websocket, String json)
|
---|
| 44 | throws Exception {
|
---|
| 45 | received = json;
|
---|
| 46 | System.out.println("received profile: "
|
---|
| 47 | + new ObjectMapper().readValue(json, Profile.class));
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public static void main(String[] args)
|
---|
| 51 | throws WebSocketException, InterruptedException, IOException {
|
---|
| 52 | new DownloadProfileExample();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | }
|
---|