mirror of
https://gitee.com/hotlcc/wechat4j.git
synced 2025-06-08 11:34:07 +08:00
提交代码
This commit is contained in:
parent
7944be3b97
commit
03bbe2d858
@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.hotlcc.wechat4j.api.WebWeixinApi;
|
import com.hotlcc.wechat4j.api.WebWeixinApi;
|
||||||
import com.hotlcc.wechat4j.enums.ExitTypeEnum;
|
import com.hotlcc.wechat4j.enums.ExitTypeEnum;
|
||||||
import com.hotlcc.wechat4j.enums.LoginTipEnum;
|
import com.hotlcc.wechat4j.enums.LoginTipEnum;
|
||||||
|
import com.hotlcc.wechat4j.handler.ExitEventHandler;
|
||||||
import com.hotlcc.wechat4j.util.CommonUtil;
|
import com.hotlcc.wechat4j.util.CommonUtil;
|
||||||
import com.hotlcc.wechat4j.util.PropertiesUtil;
|
import com.hotlcc.wechat4j.util.PropertiesUtil;
|
||||||
import com.hotlcc.wechat4j.util.QRCodeUtil;
|
import com.hotlcc.wechat4j.util.QRCodeUtil;
|
||||||
@ -26,6 +27,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
@ -61,6 +65,9 @@ public class Wechat {
|
|||||||
//同步监听器
|
//同步监听器
|
||||||
private volatile SyncMonitor syncMonitor;
|
private volatile SyncMonitor syncMonitor;
|
||||||
|
|
||||||
|
//退出事件处理器
|
||||||
|
private List<ExitEventHandler> exitEventHandlers;
|
||||||
|
|
||||||
public Wechat(CookieStore cookieStore) {
|
public Wechat(CookieStore cookieStore) {
|
||||||
this.cookieStore = cookieStore;
|
this.cookieStore = cookieStore;
|
||||||
this.httpClient = buildHttpClient(cookieStore);
|
this.httpClient = buildHttpClient(cookieStore);
|
||||||
@ -74,6 +81,21 @@ public class Wechat {
|
|||||||
this.webWeixinApi = webWeixinApi;
|
this.webWeixinApi = webWeixinApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addExitEventHandler(ExitEventHandler handler) {
|
||||||
|
if (exitEventHandlers == null) {
|
||||||
|
exitEventHandlers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
exitEventHandlers.add(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addExitEventHandler(Collection<ExitEventHandler> handlers) {
|
||||||
|
if (exitEventHandlers == null) {
|
||||||
|
exitEventHandlers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
exitEventHandlers.addAll(handlers);
|
||||||
|
}
|
||||||
|
|
||||||
private HttpClient buildHttpClient(CookieStore cookieStore) {
|
private HttpClient buildHttpClient(CookieStore cookieStore) {
|
||||||
ConnectionKeepAliveStrategy keepAliveStrategy = new DefaultConnectionKeepAliveStrategy() {
|
ConnectionKeepAliveStrategy keepAliveStrategy = new DefaultConnectionKeepAliveStrategy() {
|
||||||
@Override
|
@Override
|
||||||
@ -469,7 +491,7 @@ public class Wechat {
|
|||||||
|
|
||||||
statusNotify(time);
|
statusNotify(time);
|
||||||
isOnline = true;
|
isOnline = true;
|
||||||
syncMonitor = new SyncMonitor();
|
syncMonitor = new SyncMonitor(this);
|
||||||
syncMonitor.start();
|
syncMonitor.start();
|
||||||
} finally {
|
} finally {
|
||||||
isOnlineLock.unlock();
|
isOnlineLock.unlock();
|
||||||
@ -501,10 +523,25 @@ public class Wechat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断在线状态
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean isOnline() {
|
||||||
|
return isOnline;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信同步监听器(心跳)
|
* 微信同步监听器(心跳)
|
||||||
*/
|
*/
|
||||||
private class SyncMonitor extends Thread {
|
private class SyncMonitor extends Thread {
|
||||||
|
private Wechat wechat;
|
||||||
|
|
||||||
|
public SyncMonitor(Wechat wechat) {
|
||||||
|
this.wechat = wechat;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int time = PropertiesUtil.getIntValue("wechat4j.syncCheck.retry.time", 5);
|
int time = PropertiesUtil.getIntValue("wechat4j.syncCheck.retry.time", 5);
|
||||||
@ -567,12 +604,46 @@ public class Wechat {
|
|||||||
*/
|
*/
|
||||||
private void processExitEvent(ExitTypeEnum type, Throwable t) {
|
private void processExitEvent(ExitTypeEnum type, Throwable t) {
|
||||||
try {
|
try {
|
||||||
|
if (exitEventHandlers == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ExitEventHandler handler : exitEventHandlers) {
|
||||||
|
if (handler != null) {
|
||||||
|
processExitEvent(type, t, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Exit event process error.", e);
|
logger.error("Exit event process error.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void processExitEvent(ExitTypeEnum type, Throwable t, ExitEventHandler handler) {
|
||||||
|
try {
|
||||||
|
switch (type) {
|
||||||
|
case ERROR_EXIT:
|
||||||
|
handler.forError(wechat);
|
||||||
|
break;
|
||||||
|
case REMOTE_EXIT:
|
||||||
|
handler.forRemote(wechat);
|
||||||
|
break;
|
||||||
|
case LOCAL_EXIT:
|
||||||
|
handler.forLocal(wechat);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Exit event \"" + type.name() + "\" process error.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
handler.forAll(wechat, type, t);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("ForAll Exit event process error.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理selector值
|
* 处理selector值
|
||||||
*
|
*
|
||||||
@ -825,8 +896,4 @@ public class Wechat {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.hotlcc.wechat4j.handler;
|
||||||
|
|
||||||
|
import com.hotlcc.wechat4j.Wechat;
|
||||||
|
import com.hotlcc.wechat4j.enums.ExitTypeEnum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 退出事件处理器
|
||||||
|
*/
|
||||||
|
public interface ExitEventHandler {
|
||||||
|
/**
|
||||||
|
* 针对所有类型的退出事件
|
||||||
|
*
|
||||||
|
* @param wechat
|
||||||
|
* @param type
|
||||||
|
* @param t
|
||||||
|
*/
|
||||||
|
void forAll(Wechat wechat, ExitTypeEnum type, Throwable t);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对错误导致的退出事件
|
||||||
|
*
|
||||||
|
* @param wechat
|
||||||
|
*/
|
||||||
|
void forError(Wechat wechat);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对远程人为导致的退出事件
|
||||||
|
*
|
||||||
|
* @param wechat
|
||||||
|
*/
|
||||||
|
void forRemote(Wechat wechat);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对本地任务导致的退出事件
|
||||||
|
*
|
||||||
|
* @param wechat
|
||||||
|
*/
|
||||||
|
void forLocal(Wechat wechat);
|
||||||
|
}
|
@ -8,7 +8,6 @@ public class TestClass2 {
|
|||||||
Wechat wechat = new Wechat();
|
Wechat wechat = new Wechat();
|
||||||
wechat.setWebWeixinApi(api);
|
wechat.setWebWeixinApi(api);
|
||||||
wechat.autoLogin();
|
wechat.autoLogin();
|
||||||
wechat.test();
|
|
||||||
CommonUtil.threadSleep(1000 * 60 * 10);
|
CommonUtil.threadSleep(1000 * 60 * 10);
|
||||||
wechat.logout();
|
wechat.logout();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user