1 | package geniusweb.partiesserver;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 |
|
---|
5 | import javax.websocket.ClientEndpoint;
|
---|
6 | import javax.websocket.OnError;
|
---|
7 | import javax.websocket.OnMessage;
|
---|
8 | import javax.websocket.OnOpen;
|
---|
9 | import javax.websocket.Session;
|
---|
10 |
|
---|
11 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
12 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
13 |
|
---|
14 | import geniusweb.party.inform.Inform;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This is an example Java websocket that connects to a party on the web server.
|
---|
18 | * See {@link JavaClientTest}.
|
---|
19 | */
|
---|
20 | @ClientEndpoint
|
---|
21 | public class JavaClient {
|
---|
22 |
|
---|
23 | // this must be static because connectToServer will create new instance.
|
---|
24 | private String status = "connecting";
|
---|
25 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
26 | private Session session;
|
---|
27 |
|
---|
28 | public JavaClient() {
|
---|
29 | }
|
---|
30 |
|
---|
31 | @OnOpen
|
---|
32 | public void onOpen(Session session)
|
---|
33 | throws JsonProcessingException, IOException {
|
---|
34 | status = "connected";
|
---|
35 | this.session = session;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void sendMessage(Inform info)
|
---|
39 | throws JsonProcessingException, IOException {
|
---|
40 | session.getBasicRemote().sendText(jackson.writeValueAsString(info));
|
---|
41 | status = "sent " + info.getClass().getSimpleName();
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | @OnMessage
|
---|
46 | public void processMessage(String message) {
|
---|
47 | status = "received " + message;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @OnError
|
---|
51 | public void processError(Throwable t) {
|
---|
52 | t.printStackTrace();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public String getStatus() {
|
---|
56 | return status;
|
---|
57 | }
|
---|
58 |
|
---|
59 | }
|
---|