Skip to content

Commit

Permalink
🐛 Fixed event func override catch exception error #41
Browse files Browse the repository at this point in the history
  • Loading branch information
MisakaTAT committed Feb 16, 2022
1 parent 01b84b7 commit 64b903d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
65 changes: 34 additions & 31 deletions src/main/java/com/mikuac/shiro/handler/WebSocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.web.socket.handler.TextWebSocketHandler;

import javax.annotation.Resource;
import java.util.List;
import java.io.IOException;

/**
* Created on 2021/7/16.
Expand Down Expand Up @@ -47,14 +47,13 @@ public class WebSocketHandler extends TextWebSocketHandler {
/**
* 构造函数
*
* @param eventHandler shiroEventHandler
* @param botFactory botFactory
* @param actionHandler shiroActionHandler
* @param shiroAsyncTask asyncTask
* @param botContainer botContainer
* @param eventHandler {@link EventHandler}
* @param botFactory {@link BotFactory}
* @param actionHandler {@link ActionHandler}
* @param shiroAsyncTask {@link ShiroAsyncTask}
* @param botContainer {@link BotContainer}
*/
public WebSocketHandler(EventHandler eventHandler, BotFactory botFactory, ActionHandler actionHandler,
ShiroAsyncTask shiroAsyncTask, BotContainer botContainer) {
public WebSocketHandler(EventHandler eventHandler, BotFactory botFactory, ActionHandler actionHandler, ShiroAsyncTask shiroAsyncTask, BotContainer botContainer) {
this.eventHandler = eventHandler;
this.botFactory = botFactory;
this.actionHandler = actionHandler;
Expand All @@ -65,16 +64,19 @@ public WebSocketHandler(EventHandler eventHandler, BotFactory botFactory, Action
/**
* 获取连接的 QQ 号
*
* @param session WebSocketSession
* @param session {@link WebSocketSession}
* @return QQ 号
*/
private long parseSelfId(WebSocketSession session) {
String key = "x-self-id";
List<String> list = session.getHandshakeHeaders().get(key);
if (list == null || list.size() <= 0) {
String botId = session.getHandshakeHeaders().getFirst("x-self-id");
if (botId == null || botId.isEmpty()) {
return 0L;
}
try {
return Long.parseLong(botId);
} catch (Exception e) {
return 0L;
}
return Long.parseLong(list.get(0));
}

/**
Expand All @@ -88,32 +90,33 @@ private boolean checkToken(WebSocketSession session) {
if (token.isEmpty()) {
return true;
}
String key = "authorization";
List<String> list = session.getHandshakeHeaders().get(key);
if (list == null || list.size() <= 0) {
String clientToken = session.getHandshakeHeaders().getFirst("authorization");
if (clientToken == null || clientToken.isEmpty()) {
return false;
}
return list.get(0).contains(token);
return token.equals(clientToken);
}

@Override
public void afterConnectionEstablished(@NotNull WebSocketSession session) {
long xSelfId = parseSelfId(session);
if (xSelfId == 0L) {
log.error("Close session, get x-self-id failed.");
try {
try {
long xSelfId = parseSelfId(session);
if (xSelfId == 0L) {
log.error("Client account get failed");
session.close();
} catch (Exception e) {
log.error("Websocket session close exception");
return;
}
return;
}
if (!checkToken(session)) {
log.error("Access token check failed");
return;
if (!checkToken(session)) {
log.error("Access token check failed");
session.close();
return;
}
botContainer.robots.put(xSelfId, botFactory.createBot(xSelfId, session));
log.info("Account {} connected", xSelfId);
} catch (IOException e) {
log.error("Websocket session close exception");
e.printStackTrace();
}
botContainer.robots.put(xSelfId, botFactory.createBot(xSelfId, session));
log.info("Account {} connected", xSelfId);
}

@Override
Expand All @@ -136,7 +139,7 @@ protected void handleTextMessage(@NotNull WebSocketSession session, @NotNull Tex
}
bot.setSession(session);
JSONObject result = JSON.parseObject(message.getPayload());
// 如果返回的Json中含有echo字段说明是api响应,否则当作事件上报处理
// if resp contains echo field, this resp is action resp, else event resp.
if (result.containsKey(API_RESULT_KEY)) {
if (FAILED_STATUS.equals(result.get(RESULT_STATUS_KEY))) {
log.error("Message send failed: {}", result.get("wording"));
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/mikuac/shiro/task/ShiroAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public class ShiroAsyncTask {
*/
@Async("shiroTaskExecutor")
public void execHandlerMsg(EventHandler eventHandler, long xSelfId, JSONObject result) {
try {
eventHandler.handler(botContainer.robots.get(xSelfId), result);
} catch (Exception ignored) {
}
eventHandler.handler(botContainer.robots.get(xSelfId), result);
}

}

0 comments on commit 64b903d

Please sign in to comment.