source: src/test/java/geniusweb/partiesserver/JavaClient.java@ 41

Last change on this file since 41 was 41, checked in by bart, 3 years ago

refactor to help reusing partiesserver

File size: 1.3 KB
RevLine 
[41]1package geniusweb.partiesserver;
2
3import java.io.IOException;
4
5import javax.websocket.ClientEndpoint;
6import javax.websocket.OnError;
7import javax.websocket.OnMessage;
8import javax.websocket.OnOpen;
9import javax.websocket.Session;
10
11import com.fasterxml.jackson.core.JsonProcessingException;
12import com.fasterxml.jackson.databind.ObjectMapper;
13
14import geniusweb.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
21public 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}
Note: See TracBrowser for help on using the repository browser.