session逻辑修改

master
toesbieya 6 years ago
parent c5f6f47b92
commit 8fe65208a4
  1. 4
      java/cloud/common/src/main/java/cn/toesbieya/jxc/common/model/vo/SocketOfflineVo.java
  2. 5
      java/cloud/schedule/src/main/java/cn/toesbieya/jxc/schedule/task/SessionTask.java
  3. 2
      java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/RedisEventListenerConfig.java
  4. 33
      java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/server/WebSocketServer.java
  5. 2
      java/local/src/main/java/com/toesbieya/my/config/RedisEventListenerConfig.java
  6. 4
      java/local/src/main/java/com/toesbieya/my/model/vo/SocketOfflineVo.java
  7. 33
      java/local/src/main/java/com/toesbieya/my/module/WebSocketServer.java
  8. 5
      java/local/src/main/java/com/toesbieya/my/schedule/SessionTask.java

@ -8,8 +8,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class SocketOfflineVo {
//用户的sessionKey
private String key;
//用户ID
private Integer uid;
//断开时间
private Long time;

@ -4,7 +4,6 @@ import cn.toesbieya.jxc.common.constant.SessionConstant;
import cn.toesbieya.jxc.common.constant.SocketConstant;
import cn.toesbieya.jxc.common.model.vo.SocketOfflineVo;
import cn.toesbieya.jxc.common.utils.RedisUtil;
import cn.toesbieya.jxc.common.utils.SessionUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
@ -52,9 +51,7 @@ public class SessionTask implements CommandLineRunner {
RedisUtil.del(sessionKey);
//删除离线表信息
String token = sessionKey.replace(SessionConstant.REDIS_NAMESPACE, "");
Integer uid = SessionUtil.getUidFromToken(token);
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, String.valueOf(uid));
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, sessionKey);
}
}
});

@ -95,7 +95,7 @@ public class RedisEventListenerConfig {
String token = channel.replace(expireTopic, "");
Integer uid = SessionUtil.getUidFromToken(token);
server.logout(uid, "登陆信息过期,请重新登陆");
server.logout(uid, token, "登陆信息过期,请重新登陆");
//删除离线表信息
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, SessionConstant.REDIS_NAMESPACE + token);

@ -15,6 +15,7 @@ import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
@ -44,9 +45,12 @@ public class WebSocketServer {
public void sendEvent(String event, Integer uid, Object data, BiConsumer<UserObject, SocketIOClient> consumer) {
UserObject obj = socketMap.get(uid);
if (obj != null) {
sendEvent(event, obj, data, consumer);
}
}
if (obj == null) return;
public void sendEvent(String event, UserObject obj, Object data, BiConsumer<UserObject, SocketIOClient> consumer) {
SocketIOClient client = server.getClient(obj.getUuid());
if (client == null) return;
@ -61,8 +65,17 @@ public class WebSocketServer {
}
public void logout(Integer uid, String msg) {
sendEvent(SocketConstant.EVENT_LOGOUT, uid, msg, (obj, client) -> {
SessionUtil.remove(obj.getToken());
logout(uid, null, msg);
}
public void logout(Integer uid, String token, String msg) {
UserObject obj = socketMap.get(uid);
//如果传入token且token不同说明不是当前登陆的用户,直接返回
if (obj == null || !StringUtils.isEmpty(token) && !obj.getToken().equals(token)) return;
sendEvent(SocketConstant.EVENT_LOGOUT, obj, msg, (o, client) -> {
SessionUtil.remove(o.getToken());
client.disconnect();
});
}
@ -70,11 +83,13 @@ public class WebSocketServer {
@OnConnect
public void onConnect(SocketIOClient client) {
UserObject obj = new UserObject(client);
String sessionKey = obj.getKey();
Integer uid = obj.getUid();
//用户session是否过期?
if (!RedisUtil.exist(obj.getKey())) {
logout(uid, "登陆信息过期,请重新登陆");
if (!RedisUtil.exist(sessionKey)) {
client.sendEvent(SocketConstant.EVENT_LOGOUT, "登陆信息过期,请重新登陆");
client.disconnect();
return;
}
@ -88,7 +103,7 @@ public class WebSocketServer {
//放入在线用户ID集合
RedisUtil.sadd(SocketConstant.REDIS_ONLINE_USER, uid);
//移除离线表的信息
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, uid);
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, sessionKey);
}
@OnDisconnect
@ -114,8 +129,8 @@ public class WebSocketServer {
if (RedisUtil.exist(sessionKey)) {
RedisUtil.hset(
SocketConstant.REDIS_OFFLINE_USER,
String.valueOf(uid),
new SocketOfflineVo(sessionKey, now)
sessionKey,
new SocketOfflineVo(uid, now)
);
}
}

@ -95,7 +95,7 @@ public class RedisEventListenerConfig {
String token = channel.replace(expireTopic, "");
Integer uid = SessionUtil.getUidFromToken(token);
server.logout(uid, "登陆信息过期,请重新登陆");
server.logout(uid, token, "登陆信息过期,请重新登陆");
//删除离线表信息
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, SessionConstant.REDIS_NAMESPACE + token);

@ -8,8 +8,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class SocketOfflineVo {
//用户的sessionKey
private String key;
//用户ID
private Integer uid;
//断开时间
private Long time;

@ -15,6 +15,7 @@ import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
@ -44,9 +45,12 @@ public class WebSocketServer {
public void sendEvent(String event, Integer uid, Object data, BiConsumer<UserObject, SocketIOClient> consumer) {
UserObject obj = socketMap.get(uid);
if (obj != null) {
sendEvent(event, obj, data, consumer);
}
}
if (obj == null) return;
public void sendEvent(String event, UserObject obj, Object data, BiConsumer<UserObject, SocketIOClient> consumer) {
SocketIOClient client = server.getClient(obj.getUuid());
if (client == null) return;
@ -61,8 +65,17 @@ public class WebSocketServer {
}
public void logout(Integer uid, String msg) {
sendEvent(SocketConstant.EVENT_LOGOUT, uid, msg, (obj, client) -> {
SessionUtil.remove(obj.getToken());
logout(uid, null, msg);
}
public void logout(Integer uid, String token, String msg) {
UserObject obj = socketMap.get(uid);
//如果传入token且token不同说明不是当前登陆的用户,直接返回
if (obj == null || !StringUtils.isEmpty(token) && !obj.getToken().equals(token)) return;
sendEvent(SocketConstant.EVENT_LOGOUT, obj, msg, (o, client) -> {
SessionUtil.remove(o.getToken());
client.disconnect();
});
}
@ -70,11 +83,13 @@ public class WebSocketServer {
@OnConnect
public void onConnect(SocketIOClient client) {
UserObject obj = new UserObject(client);
String sessionKey = obj.getKey();
Integer uid = obj.getUid();
//用户session是否过期?
if (!RedisUtil.exist(obj.getKey())) {
logout(uid, "登陆信息过期,请重新登陆");
if (!RedisUtil.exist(sessionKey)) {
client.sendEvent(SocketConstant.EVENT_LOGOUT, "登陆信息过期,请重新登陆");
client.disconnect();
return;
}
@ -88,7 +103,7 @@ public class WebSocketServer {
//放入在线用户ID集合
RedisUtil.sadd(SocketConstant.REDIS_ONLINE_USER, uid);
//移除离线表的信息
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, String.valueOf(uid));
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, sessionKey);
}
@OnDisconnect
@ -114,8 +129,8 @@ public class WebSocketServer {
if (RedisUtil.exist(sessionKey)) {
RedisUtil.hset(
SocketConstant.REDIS_OFFLINE_USER,
String.valueOf(uid),
new SocketOfflineVo(sessionKey, now)
sessionKey,
new SocketOfflineVo(uid, now)
);
}
}

@ -5,7 +5,6 @@ import com.toesbieya.my.constant.SessionConstant;
import com.toesbieya.my.constant.SocketConstant;
import com.toesbieya.my.model.vo.SocketOfflineVo;
import com.toesbieya.my.utils.RedisUtil;
import com.toesbieya.my.utils.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Async;
@ -52,9 +51,7 @@ public class SessionTask {
RedisUtil.del(sessionKey);
//删除离线表信息
String token = sessionKey.replace(SessionConstant.REDIS_NAMESPACE, "");
Integer uid = SessionUtil.getUidFromToken(token);
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, String.valueOf(uid));
RedisUtil.hdel(SocketConstant.REDIS_OFFLINE_USER, sessionKey);
}
}
});

Loading…
Cancel
Save