Skip to content

Commit f29e667

Browse files
author
Javen
committed
ongoing: im client
1 parent ec0d932 commit f29e667

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
import cn.jpush.api.common.resp.APIRequestException;
2727
import cn.jpush.api.common.resp.ResponseWrapper;
2828

29+
/**
30+
* The implementation has no connection pool mechanism, used origin java connection.
31+
*
32+
* 本实现没有连接池机制,基于 Java 原始的 HTTP 连接实现。
33+
*
34+
* 遇到连接超时,会自动重连指定的次数(默认为 3);如果是读取超时,则不会自动重连。
35+
*
36+
* 可选支持 HTTP 代理,同时支持 2 种方式:1) HTTP 头上加上 Proxy-Authorization 信息;2)全局配置 Authenticator.setDefault;
37+
*/
2938
public class NativeHttpClient implements IHttpClient {
3039
private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class);
3140
private static final String KEYWORDS_CONNECT_TIMED_OUT = "connect timed out";
@@ -35,6 +44,9 @@ public class NativeHttpClient implements IHttpClient {
3544
private String _authCode;
3645
private HttpProxy _proxy;
3746

47+
/**
48+
* 默认的重连次数是 3
49+
*/
3850
public NativeHttpClient(String authCode) {
3951
this(authCode, DEFAULT_MAX_RETRY_TIMES, null);
4052
}
@@ -46,6 +58,11 @@ public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) {
4658
this._authCode = authCode;
4759
this._proxy = proxy;
4860

61+
if (_proxy.isAuthenticationNeeded()) {
62+
Authenticator.setDefault(new SimpleProxyAuthenticator(
63+
_proxy.getUsername(), _proxy.getPassword()));
64+
}
65+
4966
initSSL();
5067
}
5168

@@ -108,8 +125,6 @@ private ResponseWrapper _doRequest(String url, String content,
108125
conn = (HttpURLConnection) aUrl.openConnection(_proxy.getNetProxy());
109126
if (_proxy.isAuthenticationNeeded()) {
110127
conn.setRequestProperty("Proxy-Authorization", _proxy.getProxyAuthorization());
111-
Authenticator.setDefault(new SimpleProxyAuthenticator(
112-
_proxy.getUsername(), _proxy.getPassword()));
113128
}
114129
} else {
115130
conn = (HttpURLConnection) aUrl.openConnection();

src/main/java/cn/jpush/api/im/IMClient.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@
1616
import com.google.gson.JsonParser;
1717

1818
/**
19-
* Entrance for sending Push.
19+
* Entrance for JPush IM API.
2020
*
21-
* For the following parameters, you can set them by instance creation.
22-
* This action will override setting in PushPayload Optional.
23-
* * apnsProduction If not present, the default is true.
24-
* * timeToLive If not present, the default is 86400(s) (one day).
2521
*
26-
* Can be used directly.
2722
*/
2823
public class IMClient {
29-
public static final String HOST_NAME_SSL = "https://api.jpush.cn";
24+
public static final String HOST_NAME_SSL = ""; //"https://im.api.jpush.cn";
3025
public static final String PUSH_PATH = "/v3/push";
3126
public static final String PUSH_VALIDATE_PATH = "/v3/push/validate";
3227

3328
private final NativeHttpClient _httpClient;
34-
private JsonParser _jsonParser = new JsonParser();
29+
private static JsonParser _jsonParser = new JsonParser();
3530

3631
// If not present, true by default.
3732
private boolean _apnsProduction = true;
@@ -43,8 +38,9 @@ public class IMClient {
4338

4439
private String _baseUrl;
4540

41+
4642
/**
47-
* Create a Push Client.
43+
* Create a IM Client.
4844
*
4945
* @param masterSecret API access secret of the appKey.
5046
* @param appKey The KEY of one application on JPush.
@@ -58,7 +54,7 @@ public IMClient(String masterSecret, String appKey, int maxRetryTimes) {
5854
}
5955

6056
/**
61-
* Create a Push Client with max retry times.
57+
* Create a IM Client with max retry times.
6258
*
6359
* @param masterSecret API access secret of the appKey.
6460
* @param appKey The KEY of one application on JPush.
@@ -73,7 +69,7 @@ public IMClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy
7369
}
7470

7571
/**
76-
* Create a Push Client with global settings.
72+
* Create a IM Client with global settings.
7773
*
7874
* If you want different settings from default globally, this constructor is what you needed.
7975
*

0 commit comments

Comments
 (0)