RxWebSocketCallback.java 1.53 KB
Newer Older
1 2
package chat.rocket.android_ddp.rx;

Yusuke Iwaki's avatar
Yusuke Iwaki committed
3
import chat.rocket.android.log.RCLog;
4
import okhttp3.Response;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
5
import okhttp3.WebSocket;
6 7 8 9 10 11 12 13 14 15 16

public class RxWebSocketCallback {
  public static abstract class Base {
    public String type;
    public WebSocket ws;

    public Base(String type, WebSocket ws) {
      this.type = type;
      this.ws = ws;
    }

Yusuke Iwaki's avatar
Yusuke Iwaki committed
17 18
    @Override
    public String toString() {
19 20 21 22 23 24 25 26 27 28
      return "[" + type + "]";
    }
  }

  public static class Open extends Base {
    public Response response;

    public Open(WebSocket websocket, Response response) {
      super("Open", websocket);
      this.response = response;
29 30 31
      if (response != null && response.body() != null) {
        this.response.body().close();
      }
32 33 34 35 36 37
    }
  }

  public static class Message extends Base {
    public String responseBodyString;

Yusuke Iwaki's avatar
Yusuke Iwaki committed
38
    public Message(WebSocket websocket, String responseBody) {
39 40
      super("Message", websocket);
      try {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
41
        this.responseBodyString = responseBody;
42
      } catch (Exception e) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
43
        RCLog.e(e, "error in reading response(Message)");
44 45 46
      }
    }

Yusuke Iwaki's avatar
Yusuke Iwaki committed
47 48
    @Override
    public String toString() {
49 50 51 52 53 54 55 56 57 58 59 60 61 62
      return "[" + type + "] " + responseBodyString;
    }
  }

  public static class Close extends Base {
    public int code;
    public String reason;

    public Close(WebSocket websocket, int code, String reason) {
      super("Close", websocket);
      this.code = code;
      this.reason = reason;
    }

Yusuke Iwaki's avatar
Yusuke Iwaki committed
63 64
    @Override
    public String toString() {
65 66 67 68
      return "[" + type + "] code=" + code + ", reason=" + reason;
    }
  }
}