|
|
|
@ -14,29 +14,31 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
|
|
@Component |
|
|
|
@Component |
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
public class SocketModule { |
|
|
|
public class SocketModule { |
|
|
|
private static ConcurrentHashMap<Integer, UserClient> socketMap = new ConcurrentHashMap<>(SessionConstant.PREDICT_MAX_USER); |
|
|
|
private static final ConcurrentHashMap<Integer, UserObject> socketMap = new ConcurrentHashMap<>(SessionConstant.PREDICT_MAX_USER); |
|
|
|
private final SocketIOServer server; |
|
|
|
private static SocketIOServer server; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
public SocketModule(SocketIOServer server) { |
|
|
|
public SocketModule(SocketIOServer server) { |
|
|
|
this.server = server; |
|
|
|
SocketModule.server = server; |
|
|
|
|
|
|
|
server.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void logout(Integer uid, String msg) { |
|
|
|
public static void logout(Integer uid, String msg) { |
|
|
|
UserClient client = socketMap.get(uid); |
|
|
|
UserObject obj = socketMap.get(uid); |
|
|
|
|
|
|
|
if (obj == null) return; |
|
|
|
|
|
|
|
SocketIOClient client = server.getClient(obj.getUuid()); |
|
|
|
if (client == null) return; |
|
|
|
if (client == null) return; |
|
|
|
client.getClient().sendEvent(SocketConstant.EVENT_LOGOUT, msg); |
|
|
|
client.sendEvent(SocketConstant.EVENT_LOGOUT, msg); |
|
|
|
RedisUtil.expire(SessionConstant.REDIS_NAMESPACE + client.getSessionID()); |
|
|
|
RedisUtil.expire(SessionConstant.REDIS_NAMESPACE + obj.getSessionID()); |
|
|
|
client.getClient().disconnect(); |
|
|
|
client.disconnect(); |
|
|
|
client = null; |
|
|
|
|
|
|
|
socketMap.remove(uid); |
|
|
|
socketMap.remove(uid); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -44,27 +46,23 @@ public class SocketModule { |
|
|
|
return uid != null && socketMap.containsKey(uid); |
|
|
|
return uid != null && socketMap.containsKey(uid); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
|
|
|
public void init() { |
|
|
|
|
|
|
|
server.start(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@OnConnect |
|
|
|
@OnConnect |
|
|
|
public void onConnect(SocketIOClient client) { |
|
|
|
public void onConnect(SocketIOClient client) { |
|
|
|
UserClient userClient = new UserClient(client); |
|
|
|
UserObject obj = new UserObject(client); |
|
|
|
int uid = userClient.getUid(); |
|
|
|
Integer uid = obj.getUid(); |
|
|
|
if (socketMap.get(uid) != null) logout(uid, "该账号在其他位置登录"); |
|
|
|
if (socketMap.containsKey(uid)) logout(uid, "该账号在其他位置登录"); |
|
|
|
socketMap.put(uid, userClient); |
|
|
|
else socketMap.put(uid, obj); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@OnDisconnect |
|
|
|
@OnDisconnect |
|
|
|
public void onDisconnect(SocketIOClient client) { |
|
|
|
public void onDisconnect(SocketIOClient client) { |
|
|
|
UserClient userClient = new UserClient(client); |
|
|
|
UserObject obj = new UserObject(client); |
|
|
|
UserClient already = socketMap.get(userClient.getUid()); |
|
|
|
Integer uid = obj.getUid(); |
|
|
|
|
|
|
|
UserObject already = socketMap.get(uid); |
|
|
|
if (already == null) return; |
|
|
|
if (already == null) return; |
|
|
|
//只有sessionID相同时才移除
|
|
|
|
//只有sessionID相同时才移除
|
|
|
|
if (userClient.getSessionID().equals(already.getSessionID())) { |
|
|
|
if (obj.getSessionID().equals(already.getSessionID())) { |
|
|
|
socketMap.remove(userClient.getUid()); |
|
|
|
socketMap.remove(uid); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -83,32 +81,19 @@ public class SocketModule { |
|
|
|
return sa.substring(1, sa.indexOf(":")); |
|
|
|
return sa.substring(1, sa.indexOf(":")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int getUidFromIOClient(SocketIOClient client) { |
|
|
|
|
|
|
|
Map<String, List<String>> params = client.getHandshakeData().getUrlParams(); |
|
|
|
|
|
|
|
List<String> list = params.get("id"); |
|
|
|
|
|
|
|
if (list != null && list.size() > 0) { |
|
|
|
|
|
|
|
return Integer.parseInt(list.get(0)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
@Data |
|
|
|
private static class UserClient { |
|
|
|
private static class UserObject { |
|
|
|
private Integer uid; |
|
|
|
private Integer uid;//用户id
|
|
|
|
private String sessionID; |
|
|
|
private String sessionID;//redis中的sessionID
|
|
|
|
private SocketIOClient client; |
|
|
|
private UUID uuid;//socketClient自己的sessionID
|
|
|
|
|
|
|
|
|
|
|
|
public UserClient(SocketIOClient client) { |
|
|
|
public UserObject(SocketIOClient client) { |
|
|
|
Map<String, List<String>> params = client.getHandshakeData().getUrlParams(); |
|
|
|
Map<String, List<String>> params = client.getHandshakeData().getUrlParams(); |
|
|
|
List<String> list = params.get("id"); |
|
|
|
List<String> list = params.get("id"); |
|
|
|
if (list != null && list.size() > 0) { |
|
|
|
this.uid = Integer.valueOf(list.get(0)); |
|
|
|
this.uid = Integer.parseInt(list.get(0)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
list = params.get("session_id"); |
|
|
|
list = params.get("session_id"); |
|
|
|
if (list != null && list.size() > 0) { |
|
|
|
this.sessionID = list.get(0); |
|
|
|
this.sessionID = list.get(0); |
|
|
|
this.uuid = client.getSessionId(); |
|
|
|
} |
|
|
|
|
|
|
|
this.client = client; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|