[40] | 1 | package geniusweb.examples;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 | import java.net.URI;
|
---|
| 5 |
|
---|
| 6 | import javax.websocket.ClientEndpoint;
|
---|
| 7 | import javax.websocket.ContainerProvider;
|
---|
| 8 | import javax.websocket.DeploymentException;
|
---|
| 9 | import javax.websocket.OnError;
|
---|
| 10 | import javax.websocket.OnMessage;
|
---|
| 11 | import javax.websocket.OnOpen;
|
---|
| 12 | import javax.websocket.Session;
|
---|
| 13 | import javax.websocket.WebSocketContainer;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * This example shows how a client can download a profile from the
|
---|
| 17 | * profileserver. It uses the annotation-based javax.websocket mechanism. It
|
---|
| 18 | * assumes that the server is up and running on localhost:8080/profilesserver.
|
---|
| 19 | * We're a bit lazy here and give this example class also the annothation
|
---|
| 20 | * "@ClientEndpoint" to avoid having to create a separate class containing the
|
---|
| 21 | * actual endpoint.
|
---|
| 22 | */
|
---|
| 23 | @ClientEndpoint
|
---|
| 24 | public class DownloadProfileExample {
|
---|
| 25 |
|
---|
| 26 | // this must be static because connectToServer will create new instance.
|
---|
| 27 | private static String received = "";
|
---|
| 28 |
|
---|
| 29 | public DownloadProfileExample() {
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | @OnOpen
|
---|
| 33 | public void onOpen(Session session) {
|
---|
| 34 | System.out.println("Connected to endpoint: " + session);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @OnMessage
|
---|
| 38 | public void processMessage(String message) {
|
---|
| 39 | System.out.println("Received message in client: " + message);
|
---|
| 40 | received = message;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @OnError
|
---|
| 44 | public void processError(Throwable t) {
|
---|
| 45 | t.printStackTrace();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public void run() throws InterruptedException, IOException {
|
---|
| 49 | try {
|
---|
| 50 | WebSocketContainer container = ContainerProvider.getWebSocketContainer();
|
---|
| 51 | String uri = "ws://localhost:8080/profilesserver/websocket/get/jobs/jobs1";
|
---|
| 52 | System.out.println("Connecting to " + uri);
|
---|
| 53 | container.connectToServer(DownloadProfileExample.class, URI.create(uri));
|
---|
| 54 | Thread.sleep(2000);
|
---|
| 55 | } catch (DeploymentException | IOException ex) {
|
---|
| 56 | ex.printStackTrace();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public String getReceived() {
|
---|
| 62 | return received;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | }
|
---|