Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Commit

Permalink
修复腾讯协议变更带来的影响
Browse files Browse the repository at this point in the history
  • Loading branch information
Acexy committed Oct 31, 2018
1 parent c441370 commit 829826c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 54 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
1.1.3
升级了关键依赖版本
掉线事件回调函数返回了SmartQQClient实例,方便业务关闭实例
处理腾讯WebQQ协议变更,自己发送的好友信息会触发消息拉取事件
调整poll事件触发后使用异步线程方式去回调业务事件,防止业务事件干扰poll事件进行
1.1.2
升级部分依赖组件版本
关闭httpclient cookie检查的警告日志
Expand Down
75 changes: 38 additions & 37 deletions src/main/java/com/thankjava/wqq/core/action/SendMsgAction.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
package com.thankjava.wqq.core.action;

import com.thankjava.wqq.core.request.api.SendBuddyMsg2;
import com.thankjava.wqq.core.request.api.SendDiscuMsg2;
import com.thankjava.wqq.core.request.api.SendQunMsg2;
import com.thankjava.wqq.entity.msg.SendMsg;

public class SendMsgAction {

public boolean sendMsg(SendMsg sendMsg) {
switch (sendMsg.getMsgType()) {
case message:
return sendFriendMsg(sendMsg);
case group_message:
return sendGroupMsg(sendMsg);
case discu_message:
return sendDiscuMsg(sendMsg);
default:
return false;
}
}

private boolean sendFriendMsg(SendMsg sendMsg) {
new SendBuddyMsg2(sendMsg).doRequest(null);
return true;
}

private boolean sendGroupMsg(SendMsg sendMsg) {
new SendQunMsg2(sendMsg).doRequest(null);
return true;
}

private boolean sendDiscuMsg(SendMsg sendMsg) {
new SendDiscuMsg2(sendMsg).doRequest(null);
return true;
}
}
package com.thankjava.wqq.core.action;

import com.thankjava.toolkit3d.bean.http.AsyncResponse;
import com.thankjava.wqq.core.request.api.SendBuddyMsg2;
import com.thankjava.wqq.core.request.api.SendDiscuMsg2;
import com.thankjava.wqq.core.request.api.SendQunMsg2;
import com.thankjava.wqq.entity.msg.SendMsg;

public class SendMsgAction {

public boolean sendMsg(SendMsg sendMsg) {
switch (sendMsg.getMsgType()) {
case message:
return sendFriendMsg(sendMsg);
case group_message:
return sendGroupMsg(sendMsg);
case discu_message:
return sendDiscuMsg(sendMsg);
default:
return false;
}
}

private boolean sendFriendMsg(SendMsg sendMsg) {
new SendBuddyMsg2(sendMsg).doRequest(null);
return true;
}

private boolean sendGroupMsg(SendMsg sendMsg) {
new SendQunMsg2(sendMsg).doRequest(null);
return true;
}

private boolean sendDiscuMsg(SendMsg sendMsg) {
new SendDiscuMsg2(sendMsg).doRequest(null);
return true;
}
}
54 changes: 39 additions & 15 deletions src/main/java/com/thankjava/wqq/core/event/MsgPollEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.thankjava.wqq.core.event;

import com.thankjava.toolkit.core.reflect.ReflectUtil;
import com.thankjava.toolkit.core.thread.ThreadPool;
import com.thankjava.toolkit3d.bean.http.AsyncResponse;
import com.thankjava.wqq.WQQClient;
import com.thankjava.wqq.consts.ConfigParams;
Expand Down Expand Up @@ -28,6 +29,7 @@ public class MsgPollEvent {

private static final Logger logger = LoggerFactory.getLogger(MsgPollEvent.class);
private static Session session = Session.getSession();
private ThreadPool threadPool = new ThreadPool();
private RequestBuilder poll2 = RequestFactory.getInstance(Poll2.class);
private LoginAction loginAction = ActionFactory.getInstance(LoginAction.class);

Expand All @@ -37,24 +39,40 @@ public void poll() {

@Override
public void onListener(ActionListener actionListener) {

PullMsgStatus pullMsgStatus;

if (actionListener.getData() != null) {

AsyncResponse response = (AsyncResponse) actionListener.getData();
logger.debug("MsgPollEvent httpStatus: " + response.getHttpCode());
if (response.getHttpCode() == 200) {
PollMsg pollMsg = JSON2Entity.pollMsg(response.getDataString());
if (pollMsg != null) {
notifyMsgEvent(pollMsg);
pullMsgStatus = PullMsgStatus.normal;
if (response.getException() != null) {

logger.error("MsgPollEvent Error", response.getException());
pullMsgStatus = PullMsgStatus.http_exception;

} else {

if (response.getHttpCode() == 200) {

PollMsg pollMsg = JSON2Entity.pollMsg(response.getDataString());

if (pollMsg != null) {

notifyMsgEvent(pollMsg);
pullMsgStatus = PullMsgStatus.normal;

} else {
// 未能解析响应数据
pullMsgStatus = PullMsgStatus.http_response_error;
}

} else {
// 未能解析响应数据
pullMsgStatus = PullMsgStatus.http_response_error;
pullMsgStatus = PullMsgStatus.http_status_error;
}
} else {
pullMsgStatus = PullMsgStatus.http_status_error;
}

} else {
// http 请求异常
logger.error("MsgPollEvent error");
pullMsgStatus = PullMsgStatus.http_exception;
}

Expand All @@ -66,8 +84,14 @@ public void onListener(ActionListener actionListener) {
});
}

private void notifyMsgEvent(PollMsg pollMsg) {
WQQClient.getNotifyListener().handler(WQQClient.getInstance(), pollMsg);
private void notifyMsgEvent(final PollMsg pollMsg) {

threadPool.execute(new Runnable() {
@Override
public void run() {
WQQClient.getNotifyListener().handler(WQQClient.getInstance(), pollMsg);
}
});
}

// 用来监控&计算当前SmartQQClient的健康状态
Expand All @@ -89,14 +113,14 @@ private boolean doExceptionCheck(PullMsgStatus pullMsgStatus) {
while (retryTimes > 0) {
boolean flag = (boolean) ReflectUtil.invokeMethod(loginAction, method);
if (flag) {
logger.debug("执行重连完成");
logger.debug("系统执行自动重连完成");
break;
}
retryTimes--;
}
if (retryTimes == 0) {
// 计算是否处于连续重连失败的情况
logger.debug("执行重连失败已达到上限,已放弃尝试");
logger.debug("系统执行自动重连失败已达到上限,已放弃尝试");
CallBackListener callBackListener = WQQClient.getOfflineListener();
if (callBackListener != null) {
callBackListener.onListener(new ActionListener(WQQClient.getInstance()));
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/thankjava/wqq/util/JSON2Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,16 @@ public static PollMsg pollMsg(String content) {

JSONObject valueJson = pollMsgJson.getJSONObject("value");

// 由于腾讯协议bug造成群消息&讨论组消息,会将自己发送的消息poll识别成别人的消息,
// 此处需要过滤
// 由于腾讯协议bug造成群消息&讨论组消息,会将自己发送的消息poll识别成别人的消息
if (Session.getSession().getUin() == valueJson.getLongValue("send_uin")) {
return null;
}

// 由于腾讯协议变更,自己发送的消息会触发poll事件
if (Session.getSession().getUin() == valueJson.getLong("from_uin")) {
return null;
}

JSONArray contentArray = valueJson.getJSONArray("content");

valueJson.remove("content");
Expand Down

0 comments on commit 829826c

Please sign in to comment.