2017-03-05 02:09:37 -08:00
|
|
|
package com.baeldung.websocket;
|
|
|
|
|
|
|
|
|
|
import javax.websocket.DecodeException;
|
|
|
|
|
import javax.websocket.Decoder;
|
|
|
|
|
import javax.websocket.EndpointConfig;
|
|
|
|
|
|
|
|
|
|
import com.baeldung.model.Message;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
|
|
|
|
public class MessageDecoder implements Decoder.Text<Message> {
|
2017-03-11 13:27:41 -08:00
|
|
|
|
|
|
|
|
private static Gson gson = new Gson();
|
|
|
|
|
|
2017-03-05 02:09:37 -08:00
|
|
|
@Override
|
|
|
|
|
public Message decode(String s) throws DecodeException {
|
|
|
|
|
Message message = gson.fromJson(s, Message.class);
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean willDecode(String s) {
|
|
|
|
|
return (s != null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void init(EndpointConfig endpointConfig) {
|
|
|
|
|
// Custom initialization logic
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void destroy() {
|
|
|
|
|
// Close resources
|
|
|
|
|
}
|
|
|
|
|
}
|