source: src/test/java/geniusweb/examples/DownloadProfileExample2.java@ 13

Last change on this file since 13 was 1, checked in by bart, 5 years ago

Initial Release

File size: 1.5 KB
RevLine 
[1]1package geniusweb.examples;
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.databind.ObjectMapper;
6import com.neovisionaries.ws.client.WebSocket;
7import com.neovisionaries.ws.client.WebSocketAdapter;
8import com.neovisionaries.ws.client.WebSocketException;
9import com.neovisionaries.ws.client.WebSocketFactory;
10
11import 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 */
18public 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}
Note: See TracBrowser for help on using the repository browser.