Commit 154b1284 authored by 何金镒's avatar 何金镒

优化WebSocket

parent 62964e3d
...@@ -71,27 +71,29 @@ public class MsgWebSocket { ...@@ -71,27 +71,29 @@ public class MsgWebSocket {
*/ */
@OnOpen @OnOpen
public void onOpen(Session session) { public void onOpen(Session session) {
log.info("===>APP新连接:session-{},online-count-{},url={}",session.getId(),onlineCount.get(),session.getRequestURI());
String sn = null; String sn = null;
String ccu = null; String ccu = null;
try {
log.info("===>APP新连接:session-{},online-count-{},url={}",session.getId(),onlineCount.get(),session.getRequestURI());
if (session.getRequestParameterMap().containsKey("sn")) { if (session.getRequestParameterMap().containsKey("sn")) {
sn = session.getRequestParameterMap().get("sn").get(0); sn = session.getRequestParameterMap().get("sn").get(0);
} }
if (session.getRequestParameterMap().containsKey("ccu")) { if (session.getRequestParameterMap().containsKey("ccu")) {
ccu = session.getRequestParameterMap().get("ccu").get(0); ccu = session.getRequestParameterMap().get("ccu").get(0);
} }
if(getWebSocket(sn)){
log.error("连接被拒绝:sn已存在 - {}", sn);
session.getBasicRemote().sendText("SN already connected");
session.close();
return;
}
// 验证 sn 是否符合要求 // 验证 sn 是否符合要求
if (!this.isValidSn(sn)) { if (!this.isValidSn(sn)) {
try {
// 发送错误信息给客户端 // 发送错误信息给客户端
session.getBasicRemote().sendText("Invalid SN parameter"); session.getBasicRemote().sendText("Invalid SN parameter");
// 关闭连接 // 关闭连接
session.close(); session.close();
log.warn("连接被拒绝:无效的SN - {}", sn); log.warn("连接被拒绝:无效的SN - {}", sn);
} catch (Exception e) {
log.error("关闭无效连接时出错", e);
}
}else{ }else{
// 加入Set中 // 加入Set中
sessions.put(sn,session); sessions.put(sn,session);
...@@ -99,6 +101,19 @@ public class MsgWebSocket { ...@@ -99,6 +101,19 @@ public class MsgWebSocket {
onlineCount.getAndIncrement(); onlineCount.getAndIncrement();
//校验登录权限 //校验登录权限
} }
} catch (Exception e) {
log.error("建立连接时发生错误:SN={}",sn, e);
if (sn != null) {
sessions.remove(sn);
}
try {
if (session.isOpen()) {
session.close();
}
} catch (Exception closeException) {
log.error("关闭连接时发生错误", closeException);
}
}
} }
/** /**
...@@ -129,21 +144,56 @@ public class MsgWebSocket { ...@@ -129,21 +144,56 @@ public class MsgWebSocket {
*/ */
@OnClose @OnClose
public void onClose(Session session) { public void onClose(Session session) {
String sn = session.getRequestParameterMap().get("sn").get(0); String sn = null;
try {
if (session.getRequestParameterMap().containsKey("sn")) {
sn = session.getRequestParameterMap().get("sn").get(0);
}
if (sn != null) {
// 从Set中删除 // 从Set中删除
sessions.remove(sn); sessions.remove(sn);
// 在线数减少 // 在线数减少
onlineCount.getAndDecrement(); int currentCount = onlineCount.decrementAndGet();
log.info("session-{},down-line-count-{}",session.getId(),onlineCount.get()); log.info("连接关闭: session-{}, SN={}, 当前在线数={}",
session.getId(), sn, currentCount);
} else {
log.warn("关闭未知连接: session-{}", session.getId());
}
} catch (Exception e) {
log.error("处理连接关闭时发生错误: session-{}", session.getId(), e);
}
} }
/** /**
* 发生错误调用的方法 * 发生错误调用的方法
*/ */
@OnError @OnError
public void onError(Session session, Throwable throwable) throws Exception { public void onError(Session session, Throwable throwable) {
log.error("Web Stock Error", throwable); String sn = null;
session.getBasicRemote().sendText(throwable.getMessage()); try {
log.error("WebSocket发生错误: session-{}", session.getId(), throwable);
// 获取SN信息
if (session.getRequestParameterMap().containsKey("sn")) {
sn = session.getRequestParameterMap().get("sn").get(0);
}
// 尝试发送错误信息给客户端(如果会话仍打开)
if (session.isOpen()) {
try {
session.getBasicRemote().sendText("Connection error occurred");
} catch (Exception sendException) {
log.error("发送错误信息失败: session-{}", session.getId(), sendException);
}
}
} catch (Exception e) {
log.error("处理错误时发生异常", e);
} finally {
// 确保资源清理和计数器更新
if (sn != null) {
sessions.remove(sn);
int currentCount = onlineCount.decrementAndGet();
log.info("因错误清理连接: SN={}, 当前在线数={}", sn, currentCount);
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment