|
|
@@ -1,20 +1,8 @@
|
|
|
package com.ktg.framework.websocket;
|
|
|
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.concurrent.Semaphore;
|
|
|
-import javax.websocket.OnClose;
|
|
|
-import javax.websocket.OnError;
|
|
|
-import javax.websocket.OnMessage;
|
|
|
-import javax.websocket.OnOpen;
|
|
|
-import javax.websocket.Session;
|
|
|
-import javax.websocket.server.PathParam;
|
|
|
-import javax.websocket.server.ServerEndpoint;
|
|
|
-
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.ktg.common.constant.UserConstants;
|
|
|
-import com.ktg.common.core.domain.entity.SysUser;
|
|
|
import com.ktg.common.core.domain.model.LoginUser;
|
|
|
import com.ktg.common.utils.StringUtils;
|
|
|
import com.ktg.common.utils.spring.SpringUtils;
|
|
|
@@ -22,9 +10,14 @@ import com.ktg.framework.web.service.TokenService;
|
|
|
import com.ktg.system.domain.SysMessage;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.websocket.*;
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.Semaphore;
|
|
|
+
|
|
|
/**
|
|
|
* websocket 消息处理
|
|
|
*
|
|
|
@@ -32,8 +25,7 @@ import org.springframework.stereotype.Component;
|
|
|
*/
|
|
|
@Component
|
|
|
@ServerEndpoint("/websocket/message/{token}")
|
|
|
-public class WebSocketServer
|
|
|
-{
|
|
|
+public class WebSocketServer {
|
|
|
/**
|
|
|
* WebSocketServer 日志控制器
|
|
|
*/
|
|
|
@@ -47,25 +39,27 @@ public class WebSocketServer
|
|
|
|
|
|
private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount);
|
|
|
|
|
|
- /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
|
|
|
+ /**
|
|
|
+ * 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
|
|
|
+ */
|
|
|
private static int onlineCount = 0;
|
|
|
|
|
|
/**
|
|
|
* concurrent包的线程安全set,用来存放每个客户端对应的MyWebSocket对象
|
|
|
*/
|
|
|
- private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap();
|
|
|
+ private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap();
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 连接建立成功调用的方法
|
|
|
*/
|
|
|
@OnOpen
|
|
|
- public void onOpen(Session session,@PathParam("token") String token) throws Exception
|
|
|
- {
|
|
|
+ public void onOpen(Session session, @PathParam("token") String token) throws Exception {
|
|
|
LOGGER.info("[连接token:{}] 建立连接, 当前连接数:{}", token, webSocketMap.size());
|
|
|
boolean semaphoreFlag = false;
|
|
|
//身份验证
|
|
|
- if(!StringUtils.isNotNull(token)){
|
|
|
+ if (!StringUtils.isNotNull(token)) {
|
|
|
+ LOGGER.error("当前token为空!");
|
|
|
session.close();
|
|
|
return;
|
|
|
}
|
|
|
@@ -73,27 +67,25 @@ public class WebSocketServer
|
|
|
TokenService tokenService = SpringUtils.getBean(TokenService.class);
|
|
|
|
|
|
LoginUser user = tokenService.getUserByToken(token);
|
|
|
- if(!StringUtils.isNotNull(user)){
|
|
|
+ if (!StringUtils.isNotNull(user)) {
|
|
|
+ LOGGER.error("当前user不存在!");
|
|
|
session.close();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 尝试获取信号量
|
|
|
semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore);
|
|
|
- if (!semaphoreFlag)
|
|
|
- {
|
|
|
+ if (!semaphoreFlag) {
|
|
|
// 未获取到信号量
|
|
|
LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount);
|
|
|
WebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount);
|
|
|
session.close();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
if (webSocketMap.containsKey(token)) {
|
|
|
webSocketMap.remove(token);
|
|
|
- webSocketMap.put(token,this);
|
|
|
+ webSocketMap.put(token, this);
|
|
|
} else {
|
|
|
- webSocketMap.put(token,this);
|
|
|
+ webSocketMap.put(token, this);
|
|
|
addOnlineCount();
|
|
|
}
|
|
|
|
|
|
@@ -109,8 +101,7 @@ public class WebSocketServer
|
|
|
* 连接关闭时处理
|
|
|
*/
|
|
|
@OnClose
|
|
|
- public void onClose(Session session)
|
|
|
- {
|
|
|
+ public void onClose(Session session) {
|
|
|
LOGGER.info("\n 关闭连接 - {}", session);
|
|
|
// 移除用户
|
|
|
WebSocketUsers.remove(session);
|
|
|
@@ -122,10 +113,8 @@ public class WebSocketServer
|
|
|
* 抛出异常时处理
|
|
|
*/
|
|
|
@OnError
|
|
|
- public void onError(Session session, Throwable exception) throws Exception
|
|
|
- {
|
|
|
- if (session.isOpen())
|
|
|
- {
|
|
|
+ public void onError(Session session, Throwable exception) throws Exception {
|
|
|
+ if (session.isOpen()) {
|
|
|
// 关闭连接
|
|
|
session.close();
|
|
|
}
|
|
|
@@ -142,16 +131,16 @@ public class WebSocketServer
|
|
|
* 服务器接收到客户端消息时调用的方法
|
|
|
*/
|
|
|
@OnMessage
|
|
|
- public void onMessage(String message, Session session)
|
|
|
- {
|
|
|
- if(!UserConstants.WEBSOCKET_HEARTBEAT.equals(message)){
|
|
|
- try{
|
|
|
- SysMessage msg = JSON.parseObject(message, new TypeReference<SysMessage>(){});
|
|
|
- if(StringUtils.isNotNull(msg.getRecipientName())){
|
|
|
+ public void onMessage(String message, Session session) {
|
|
|
+ if (!UserConstants.WEBSOCKET_HEARTBEAT.equals(message)) {
|
|
|
+ try {
|
|
|
+ SysMessage msg = JSON.parseObject(message, new TypeReference<SysMessage>() {
|
|
|
+ });
|
|
|
+ if (StringUtils.isNotNull(msg.getRecipientName())) {
|
|
|
//这里必须传递username
|
|
|
- WebSocketUsers.sendMesssageToUserByName(msg.getRecipientName(),message);
|
|
|
+ WebSocketUsers.sendMesssageToUserByName(msg.getRecipientName(), message);
|
|
|
}
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
LOGGER.error("\n 错误的websocket信息格式 - {}", message);
|
|
|
}
|
|
|
}
|