package geniusweb.partiesserver; import java.io.IOException; import javax.websocket.ClientEndpoint; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.party.inform.Inform; /** * This is an example Java websocket that connects to a party on the web server. * See {@link JavaClientTest}. */ @ClientEndpoint public class JavaClient { // this must be static because connectToServer will create new instance. private String status = "connecting"; private final static ObjectMapper jackson = new ObjectMapper(); private Session session; public JavaClient() { } @OnOpen public void onOpen(Session session) throws JsonProcessingException, IOException { status = "connected"; this.session = session; } public void sendMessage(Inform info) throws JsonProcessingException, IOException { session.getBasicRemote().sendText(jackson.writeValueAsString(info)); status = "sent " + info.getClass().getSimpleName(); } @OnMessage public void processMessage(String message) { status = "received " + message; } @OnError public void processError(Throwable t) { t.printStackTrace(); } public String getStatus() { return status; } }