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

Yusuke Iwaki's avatar
Yusuke Iwaki committed
3 4 5
import static android.R.attr.type;

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

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
19 20
    @Override
    public String toString() {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
      return "[" + type + "]";
    }
  }

  public static class Open extends Base {
    public Response response;

    public Open(WebSocket websocket, Response response) {
      super("Open", websocket);
      this.response = response;
    }
  }

  public static class Failure extends Exception {
    public WebSocket ws;
    public Response response;

Yusuke Iwaki's avatar
Yusuke Iwaki committed
38 39
    public Failure(WebSocket websocket, Throwable err, Response response) {
      super(err);
40 41 42 43
      this.ws = websocket;
      this.response = response;
    }

Yusuke Iwaki's avatar
Yusuke Iwaki committed
44 45
    @Override
    public String toString() {
46 47 48 49 50 51 52 53 54 55 56
      if (response != null) {
        return "[" + type + "] " + response.message();
      } else {
        return super.toString();
      }
    }
  }

  public static class Message extends Base {
    public String responseBodyString;

Yusuke Iwaki's avatar
Yusuke Iwaki committed
57
    public Message(WebSocket websocket, String responseBody) {
58 59
      super("Message", websocket);
      try {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
60
        this.responseBodyString = responseBody;
61
      } catch (Exception e) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
62
        RCLog.e(e, "error in reading response(Message)");
63 64 65
      }
    }

Yusuke Iwaki's avatar
Yusuke Iwaki committed
66 67
    @Override
    public String toString() {
68 69 70 71 72 73 74 75 76 77 78 79 80 81
      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
82 83
    @Override
    public String toString() {
84 85 86 87
      return "[" + type + "] code=" + code + ", reason=" + reason;
    }
  }
}