修改redis不会关闭问题

master
zhouwei 2026-02-06 15:18:15 +08:00
parent 5a3ab7efda
commit 263a7b9724
5 changed files with 42 additions and 11 deletions

View File

@ -260,9 +260,10 @@ public class EXGameController extends GameController {
* */
public void webGroupJoinRoom(RobotUser robotUser) {
String connecId = robotUser.getConnecId();
Jedis jedis0 = Redis.use("group1_db0").getJedis();
Jedis jedis2 = Redis.use("group1_db2").getJedis();
//重启检查
try (Jedis jedis0 = Redis.use("group1_db0").getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis()){
try {
Set<String> robotTokens = jedis0.smembers("{user}:"+robotUser.getRobotId()+"_token");
String robotSession = null;
@ -287,6 +288,9 @@ public class EXGameController extends GameController {
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
jedis0.close();
jedis2.close();
}
}
@ -294,7 +298,9 @@ public class EXGameController extends GameController {
*
*/
private void joinRoomCommon(int robotId, String roomId, int groupId, ITObject params) {
try (Jedis jedis0 = Redis.use("group1_db0").getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis()) {
Jedis jedis0 = Redis.use("group1_db0").getJedis();
Jedis jedis2 = Redis.use("group1_db2").getJedis();
try {
Set<String> robotTokens = jedis0.smembers("{user}:" + robotId + "_token");
String robotSession = null;
@ -343,6 +349,9 @@ public class EXGameController extends GameController {
System.err.println("已进入房间准备成功: " + robotUser.getConnecId());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
jedis0.close();
jedis2.close();
}
}

View File

@ -112,6 +112,8 @@ public class EXMainServer extends MainServer{
//5、干活
log.info("长沙麻将机器人服务器已启动");
log.info("服务器将监听端口 {} 用于接收robot_mgr管理协议", gameSetting.port);
jedis2.close();
}
/**

View File

@ -318,7 +318,9 @@ public class RobotConnectionManager {
int robotId = Integer.parseInt(robotUser.getRobotId());
ITObject param = message.param;
HuNanChangSha huNanChangSha = getHuNanChangShaInstance(connecId);
try (Jedis jedis0 = Redis.use().getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis();){
Jedis jedis0 = Redis.use().getJedis();
Jedis jedis2 = Redis.use("group1_db2").getJedis();
try {
//长沙麻将 机器人处理事件
//初始化收手牌
if ("811".equalsIgnoreCase(command)) {
@ -675,6 +677,9 @@ public class RobotConnectionManager {
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
jedis0.close();
jedis2.close();
}
}
@ -682,13 +687,16 @@ public class RobotConnectionManager {
* leftover_robot 退
*/
private void updateLeftoverRobot(int robotId) {
try (Jedis jedis2 = Redis.use("group1_db2").getJedis()) {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
try {
jedis2.hset("gallrobot", String.valueOf(robotId), "0");
jedis2.hset("{grobot}:" + robotId, "start", "0");
System.out.println("机器人 {"+robotId+"} 退出房间修改gallrobot为0");
} finally {
jedis2.close();
}
}

View File

@ -112,7 +112,8 @@ public class HuNanChangSha {
* @param connecId ID
*/
public void saveToRedis(String connecId) {
try (Jedis jedis = Redis.use("group1_db2").getJedis()) {
Jedis jedis = Redis.use("group1_db2").getJedis();
try {
Map<String, String> stateMap = new HashMap<>();
stateMap.put("changShaCardInhand", gson.toJson(changShaCardInhand));
@ -135,6 +136,8 @@ public class HuNanChangSha {
System.out.println("保存HuNanChangSha状态到Redis: " + connecId);
} catch (Exception e) {
System.err.println("保存HuNanChangSha状态到Redis失败: " + e.getMessage());
} finally {
jedis.close();
}
}
@ -144,7 +147,8 @@ public class HuNanChangSha {
* @return
*/
public boolean restoreFromRedis(String connecId) {
try (Jedis jedis = Redis.use("group1_db2").getJedis()) {
Jedis jedis = Redis.use("group1_db2").getJedis();
try {
String redisKey = "{csmj}:" + connecId;
Map<String, String> stateMap = jedis.hgetAll(redisKey);
@ -216,6 +220,8 @@ public class HuNanChangSha {
} catch (Exception e) {
System.err.println("从Redis恢复HuNanChangSha状态失败: " + e.getMessage());
return false;
} finally {
jedis.close();
}
}
@ -224,12 +230,15 @@ public class HuNanChangSha {
* @param connecId ID
*/
public static void removeFromRedis(String connecId) {
try (Jedis jedis = Redis.use("group1_db2").getJedis()) {
Jedis jedis = Redis.use("group1_db2").getJedis();
try {
String redisKey = "{csmj}:" + connecId;
jedis.del(redisKey);
System.out.println("从Redis删除HuNanChangSha状态: " + connecId);
} catch (Exception e) {
System.err.println("从Redis删除HuNanChangSha状态失败: " + e.getMessage());
} finally {
jedis.close();
}
}
@ -1990,12 +1999,15 @@ public class HuNanChangSha {
List<Integer> robotIdsList = new ArrayList<>();
//Redis中获取当前活跃的机器人连接信息
try (Jedis jedis = Redis.use("group1_db2").getJedis()) {
Jedis jedis = Redis.use("group1_db2").getJedis();
try {
//从Redis中获取所有在线的机器人ID
java.util.Set<String> allKeys = jedis.keys("{robot}:*");
Set<String> allKeys = jedis.keys("{robot}:*");
for (String key : allKeys) {
robotIdsList.add(Integer.valueOf(key.replace("{robot}:","")));
}
} finally {
jedis.close();
}
if (!robotIdsList.contains(playerid)) {

View File

@ -5,7 +5,7 @@ import com.taurus.permanent.TPServer;
public class Main {
public static void main(String[] args) {
System.out.println("启动长沙麻将机器人服务器...");
System.out.println("服务器将监听端口7701用于游戏协议");
System.out.println("服务器将监听端口8701用于游戏协议");
//启动机器人服务
TPServer.me().start();