Skip to content
This repository was archived by the owner on Jun 5, 2018. It is now read-only.

Commit 6a4a03b

Browse files
committed
fix #27
1 parent 50ddae4 commit 6a4a03b

File tree

3 files changed

+119
-16
lines changed

3 files changed

+119
-16
lines changed

src/main/java/me/biezhi/wechat/service/WechatService.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ public interface WechatService {
1212
* 获取UUID
1313
* @return
1414
*/
15-
String getUUID() throws WechatException;
15+
String getUUID();
1616

1717
/**
1818
* 微信初始化
1919
* @param wechatMeta
2020
* @throws WechatException
2121
*/
22-
void wxInit(WechatMeta wechatMeta) throws WechatException;
22+
void wxInit(WechatMeta wechatMeta);
2323

2424
/**
2525
* 开启状态通知
2626
* @return
2727
*/
28-
void openStatusNotify(WechatMeta wechatMeta) throws WechatException;
28+
void openStatusNotify(WechatMeta wechatMeta);
2929

3030
/**
3131
* 获取联系人
3232
* @param wechatMeta
3333
* @return
3434
*/
35-
WechatContact getContact(WechatMeta wechatMeta) throws WechatException;
35+
WechatContact getContact(WechatMeta wechatMeta);
3636

3737
/**
3838
* 选择同步线路
@@ -41,26 +41,26 @@ public interface WechatService {
4141
* @return
4242
* @throws WechatException
4343
*/
44-
void choiceSyncLine(WechatMeta wechatMeta) throws WechatException;
44+
void choiceSyncLine(WechatMeta wechatMeta);
4545

4646
/**
4747
* 消息检查
4848
* @param wechatMeta
4949
* @return
5050
*/
51-
int[] syncCheck(WechatMeta wechatMeta) throws WechatException;
51+
int[] syncCheck(WechatMeta wechatMeta);
5252

5353
/**
5454
* 处理聊天信息
55-
* @param wechatRequest
55+
* @param wechatMeta
5656
* @param data
5757
*/
58-
void handleMsg(WechatMeta wechatMeta, JSONObject data) throws WechatException;
58+
void handleMsg(WechatMeta wechatMeta, JSONObject data);
5959

6060
/**
6161
* 获取最新消息
6262
* @param meta
6363
* @return
6464
*/
65-
JSONObject webwxsync(WechatMeta meta) throws WechatException;
65+
JSONObject webwxsync(WechatMeta meta);
6666
}

src/main/java/me/biezhi/wechat/service/WechatServiceImpl.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package me.biezhi.wechat.service;
22

33
import java.io.File;
4+
import java.util.concurrent.TimeUnit;
45

6+
import me.biezhi.wechat.util.PingUtil;
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79

@@ -164,7 +166,7 @@ private void getGroup(WechatMeta wechatMeta, WechatContact wechatContact) {
164166
* 获取UUID
165167
*/
166168
@Override
167-
public String getUUID() throws WechatException {
169+
public String getUUID() {
168170
HttpRequest request = HttpRequest.get(Constant.JS_LOGIN_URL, true, "appid", "wx782c26e4c19acffb", "fun", "new",
169171
"lang", "zh_CN", "_", DateKit.getCurrentUnixTime());
170172

@@ -190,7 +192,7 @@ public String getUUID() throws WechatException {
190192
* 打开状态提醒
191193
*/
192194
@Override
193-
public void openStatusNotify(WechatMeta wechatMeta) throws WechatException {
195+
public void openStatusNotify(WechatMeta wechatMeta) {
194196

195197
String url = wechatMeta.getBase_uri() + "/webwxstatusnotify?lang=zh_CN&pass_ticket=" + wechatMeta.getPass_ticket();
196198

@@ -230,7 +232,7 @@ public void openStatusNotify(WechatMeta wechatMeta) throws WechatException {
230232
* 微信初始化
231233
*/
232234
@Override
233-
public void wxInit(WechatMeta wechatMeta) throws WechatException {
235+
public void wxInit(WechatMeta wechatMeta) {
234236
String url = wechatMeta.getBase_uri() + "/webwxinit?r=" + DateKit.getCurrentUnixTime() + "&pass_ticket="
235237
+ wechatMeta.getPass_ticket() + "&skey=" + wechatMeta.getSkey();
236238

@@ -276,7 +278,7 @@ public void wxInit(WechatMeta wechatMeta) throws WechatException {
276278
* 选择同步线路
277279
*/
278280
@Override
279-
public void choiceSyncLine(WechatMeta wechatMeta) throws WechatException {
281+
public void choiceSyncLine(WechatMeta wechatMeta) {
280282
boolean enabled = false;
281283
for(String syncUrl : Constant.SYNC_HOST){
282284
int[] res = this.syncCheck(syncUrl, wechatMeta);
@@ -297,14 +299,24 @@ public void choiceSyncLine(WechatMeta wechatMeta) throws WechatException {
297299
* 检测心跳
298300
*/
299301
@Override
300-
public int[] syncCheck(WechatMeta wechatMeta) throws WechatException{
302+
public int[] syncCheck(WechatMeta wechatMeta){
301303
return this.syncCheck(null, wechatMeta);
302304
}
303305

304306
/**
305307
* 检测心跳
306308
*/
307-
private int[] syncCheck(String url, WechatMeta meta) throws WechatException{
309+
private int[] syncCheck(String url, WechatMeta meta){
310+
311+
// 如果网络中断,休息10秒
312+
if(PingUtil.netIsOver()){
313+
try {
314+
TimeUnit.SECONDS.sleep(10);
315+
} catch (Exception e){
316+
LOGGER.error("", e);
317+
}
318+
}
319+
308320
if(null == url){
309321
url = meta.getWebpush_url() + "/synccheck";
310322
} else{
@@ -435,7 +447,7 @@ private String getUserRemarkName(String id) {
435447
}
436448

437449
@Override
438-
public JSONObject webwxsync(WechatMeta meta) throws WechatException{
450+
public JSONObject webwxsync(WechatMeta meta){
439451

440452
String url = meta.getBase_uri() + "/webwxsync?skey=" + meta.getSkey() + "&sid=" + meta.getWxsid();
441453

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package me.biezhi.wechat.util;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.InetAddress;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
public class PingUtil {
11+
12+
public static boolean ping(String ipAddress) throws Exception {
13+
int timeOut = 3000 ; //超时应该在3钞以上
14+
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
15+
return status;
16+
}
17+
18+
/**
19+
* 返回是否能ping通
20+
* @param ipAddress
21+
* @return
22+
* @throws Exception
23+
*/
24+
public static boolean ping02(String ipAddress) throws Exception {
25+
String line = null;
26+
try {
27+
Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
28+
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
29+
while ((line = buf.readLine()) != null){
30+
if(line.startsWith("PING")){
31+
continue;
32+
}
33+
if(line.indexOf("timeout") != -1){
34+
return false;
35+
}
36+
return true;
37+
}
38+
return false;
39+
} catch (Exception ex) {
40+
return false;
41+
}
42+
}
43+
44+
public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
45+
BufferedReader in = null;
46+
Runtime r = Runtime.getRuntime(); // 将要执行的ping命令,此命令是windows格式的命令
47+
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
48+
try { // 执行命令并获取输出
49+
System.out.println(pingCommand);
50+
Process p = r.exec(pingCommand);
51+
if (p == null) {
52+
return false;
53+
}
54+
in = new BufferedReader(new InputStreamReader(p.getInputStream())); // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
55+
int connectedCount = 0;
56+
String line = null;
57+
while ((line = in.readLine()) != null) {
58+
connectedCount += getCheckResult(line);
59+
} // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
60+
return connectedCount == pingTimes;
61+
} catch (Exception ex) {
62+
ex.printStackTrace(); // 出现异常则返回假
63+
return false;
64+
} finally {
65+
try {
66+
in.close();
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
}
72+
//若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
73+
private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+line);
74+
Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
75+
Matcher matcher = pattern.matcher(line);
76+
while (matcher.find()) {
77+
return 1;
78+
}
79+
return 0;
80+
}
81+
82+
public static boolean netIsOver(){
83+
try {
84+
return !ping02("114.114.114.114");
85+
} catch (Exception e){
86+
return true;
87+
}
88+
}
89+
90+
91+
}

0 commit comments

Comments
 (0)