拆分think模块包
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.gxwebsoft.common.core.socketio.cache;
|
||||
|
||||
import com.corundumstudio.socketio.SocketIOClient;
|
||||
import com.corundumstudio.socketio.SocketIOServer;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @Author
|
||||
* @Description 用户信息缓存
|
||||
* @Date 14:00 2022/1/21
|
||||
* @Param
|
||||
* @return
|
||||
**/
|
||||
@Component
|
||||
public class ClientCache {
|
||||
|
||||
private static Map<String, HashMap<UUID, SocketIOClient>> concurrentHashMap = new ConcurrentHashMap<>();
|
||||
|
||||
private static SocketIOServer socketIOServer;
|
||||
|
||||
public static SocketIOServer getSocketIOServer() {
|
||||
return socketIOServer;
|
||||
}
|
||||
|
||||
public static void setSocketIOServer(SocketIOServer instance) {
|
||||
socketIOServer = instance;
|
||||
}
|
||||
public void saveClient(String userId,UUID sessionId,SocketIOClient socketIOClient){
|
||||
HashMap<UUID, SocketIOClient> sessionIdClientCache = concurrentHashMap.get(userId);
|
||||
if(sessionIdClientCache == null){
|
||||
sessionIdClientCache = new HashMap<>();
|
||||
}
|
||||
sessionIdClientCache.put(sessionId,socketIOClient);
|
||||
concurrentHashMap.put(userId,sessionIdClientCache);
|
||||
}
|
||||
|
||||
|
||||
public HashMap<UUID,SocketIOClient> getUserClient(String userId){
|
||||
return concurrentHashMap.get(userId);
|
||||
}
|
||||
|
||||
public void deleteSessionClientByUserId(String userId,UUID sessionId){
|
||||
concurrentHashMap.get(userId).remove(sessionId);
|
||||
}
|
||||
|
||||
|
||||
public void deleteUserCacheByUserId(String userId){
|
||||
concurrentHashMap.remove(userId);
|
||||
}
|
||||
|
||||
public int getOnLineCount(){
|
||||
return concurrentHashMap.size();
|
||||
}
|
||||
|
||||
public void sendUserEvent(String userId,String event, Object message) {
|
||||
// 发送到接收方
|
||||
HashMap<UUID, SocketIOClient> userClient = concurrentHashMap.get(userId);
|
||||
|
||||
// 查看对方是否在线
|
||||
if(!CollectionUtils.isEmpty(userClient)){
|
||||
for (UUID uuid : userClient.keySet()) {
|
||||
SocketIOClient ioClient = userClient.get(uuid);
|
||||
ioClient.sendEvent(event, message);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.gxwebsoft.common.core.socketio.config;
|
||||
|
||||
import com.corundumstudio.socketio.SocketIOServer;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.security.JwtSubject;
|
||||
import com.gxwebsoft.common.core.security.JwtUtil;
|
||||
import com.gxwebsoft.common.core.socketio.cache.ClientCache;
|
||||
import com.gxwebsoft.common.core.socketio.handler.SocketIOHandler;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* socket服务配置
|
||||
* @author machenike
|
||||
*/
|
||||
@Configuration
|
||||
public class SocketIOConfig implements InitializingBean {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(SocketIOConfig.class);
|
||||
@Value("${socketio.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${socketio.port}")
|
||||
private Integer port;
|
||||
|
||||
@Resource
|
||||
private SocketIOHandler socketIOHandler;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
|
||||
//设置host
|
||||
config.setHostname(host);
|
||||
//设置端口
|
||||
config.setPort(port);
|
||||
config.setBossThreads(1);
|
||||
config.setAuthorizationListener(handshakeData -> {
|
||||
String userId =handshakeData.getSingleUrlParam("userId");
|
||||
String token = handshakeData.getSingleUrlParam("token");
|
||||
logger.info("身份验证 token:{}", token);
|
||||
if(!StringUtils.hasText(token) || !StringUtils.hasText(userId)){
|
||||
return false;
|
||||
}
|
||||
// 身份验证
|
||||
Claims claims = JwtUtil.parseToken(token, configProperties.getTokenKey());
|
||||
JwtSubject jwtSubject = JwtUtil.getJwtSubject(claims);
|
||||
User user = userService.getByUsername(jwtSubject.getUsername(), jwtSubject.getTenantId());
|
||||
|
||||
if (user == null || !user.getUserId().equals(Integer.valueOf(userId))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
InputStream resourceAsStream = this.getClass().getResourceAsStream("/jks/love.jks"); // 读取证书文件流
|
||||
config.setKeyStore(resourceAsStream); // 设置证书文件
|
||||
config.setKeyStorePassword("123456"); // 设置证书密码
|
||||
|
||||
// 启动socket服务
|
||||
// SocketIOServer server = new SocketIOServer(config);
|
||||
// server.addListeners(socketIOHandler);
|
||||
// server.start();
|
||||
// ClientCache.setSocketIOServer(server);
|
||||
// logger.debug("Netty SocketIO启动:{}:{}",host,port);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.gxwebsoft.common.core.socketio.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.corundumstudio.socketio.AckRequest;
|
||||
import com.corundumstudio.socketio.SocketIOClient;
|
||||
import com.corundumstudio.socketio.annotation.OnConnect;
|
||||
import com.corundumstudio.socketio.annotation.OnDisconnect;
|
||||
import com.corundumstudio.socketio.annotation.OnEvent;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.socketio.cache.ClientCache;
|
||||
import com.gxwebsoft.common.system.entity.ChatMessage;
|
||||
import com.gxwebsoft.common.system.service.ChatConversationService;
|
||||
import com.gxwebsoft.common.system.service.ChatMessageService;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
/**
|
||||
* socket处理拦截器
|
||||
* @author machenike
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class SocketIOHandler {
|
||||
|
||||
|
||||
@Resource
|
||||
private ChatMessageService messageService;
|
||||
|
||||
@Resource
|
||||
private ChatConversationService conversationService;
|
||||
|
||||
@Resource
|
||||
private ClientCache clientCache;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
/**
|
||||
* 日志
|
||||
*/
|
||||
private final static Logger logger = LoggerFactory.getLogger(SocketIOHandler.class);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 客户端连上socket服务器时执行此事件
|
||||
* @param client
|
||||
*/
|
||||
@OnConnect
|
||||
public void onConnect(SocketIOClient client) {
|
||||
String userId = client.getHandshakeData().getSingleUrlParam("userId");
|
||||
logger.debug("socket client auth success [userId="+userId+"]");
|
||||
UUID sessionId = client.getSessionId();
|
||||
|
||||
|
||||
// 管理员
|
||||
String isAdmin = client.getHandshakeData().getSingleUrlParam("isAdmin");
|
||||
if(StringUtils.isNotBlank(isAdmin)){
|
||||
// todo 权限验证
|
||||
clientCache.saveClient("admin",sessionId, client);
|
||||
}else {
|
||||
clientCache.saveClient(userId,sessionId, client);
|
||||
}
|
||||
|
||||
System.out.println("userId: "+userId+"连接建立成功 - "+sessionId);
|
||||
logger.info("当前在线人数:{}",clientCache.getOnLineCount());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 客户端断开socket服务器时执行此事件
|
||||
* @param client
|
||||
*/
|
||||
@OnDisconnect
|
||||
public void onDisconnect(SocketIOClient client) {
|
||||
String userId = client.getHandshakeData().getSingleUrlParam("userId");
|
||||
UUID sessionId = client.getSessionId();
|
||||
clientCache.deleteSessionClientByUserId(userId,sessionId);
|
||||
System.out.println("userId: "+userId+"连接关闭成功 - "+sessionId);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param client
|
||||
*/
|
||||
@OnEvent( value = "message")
|
||||
@Transactional
|
||||
public void onMessage(SocketIOClient client, AckRequest request, ChatMessage message) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user