コード例 #1
0
ファイル: Pubnub.java プロジェクト: nvdnkpr/pubnub-api
  /**
   * Subscribe - Private Interface
   *
   * <p>Patch provided by petereddy on GitHub
   *
   * @param String channel name.
   * @param Callback function callback.
   * @param String timetoken.
   */
  private void _subscribe(String channel, Callback callback, String timetoken) {
    while (true) {
      try {
        // Build URL
        List<String> url =
            java.util.Arrays.asList("subscribe", this.SUBSCRIBE_KEY, channel, "0", timetoken);

        // Wait for Message
        JSONArray response = _request(url);
        JSONArray messages = response.optJSONArray(0);

        // Update TimeToken
        if (response.optString(1).length() > 0) timetoken = response.optString(1);

        // Run user Callback and Reconnect if user permits. If
        // there's a timeout then messages.length() == 0.
        for (int i = 0; messages.length() > i; i++) {
          JSONObject message = messages.optJSONObject(i);
          if (!callback.execute(message) || cancel) return;
        }
      } catch (Exception e) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException ie) {
          return;
        }
      }
    }
  }
コード例 #2
0
ファイル: Pubnub.java プロジェクト: nvdnkpr/pubnub-api
  public void history(String channel, int limit, Callback callback) {
    List<String> url = new ArrayList<String>();

    url.add("history");
    url.add(this.SUBSCRIBE_KEY);
    url.add(channel);
    url.add("0");
    url.add(Integer.toString(limit));

    JSONArray messages = _request(url);

    // Run user Callback and Reconnect if user permits. If
    // there's a timeout then messages.length() == 0.
    for (int i = 0; messages.length() > i; i++) {
      JSONObject message = messages.optJSONObject(i);

      if (!callback.execute(message) || cancel) return;
    }
  }