Pig Cloud遇到websocket不能实现同一个用户不同浏览器接受到广播的消息解决方案

自定义SecuritySessionKeyGenerator类,为每个客户端连接建立唯一的key

java 复制代码
package com.pig4cloud.plugin.websocket.custom;

import com.pig4cloud.plugin.websocket.holder.SessionKeyGenerator;
import org.springframework.web.socket.WebSocketSession;

import java.util.UUID;
public class SecuritySessionKeyGenerator implements SessionKeyGenerator {
	@Override
	public Object sessionKey(WebSocketSession webSocketSession) {
		Object user = webSocketSession.getAttributes().get("USER_KEY_ATTR_NAME");
		// 添加随机后缀使每个连接键唯一
		return user != null ? user + "-" + UUID.randomUUID().toString() : null;
	}
}

重写WebSocketSessionHolder类方法

java 复制代码
package com.pig4cloud.plugin.websocket.holder;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.springframework.web.socket.WebSocketSession;

public final class WebSocketSessionHolder {
	// 仅修改这一行:将Value改为Set类型
	private static final Map<String, Set<WebSocketSession>> USER_SESSION_MAP = new ConcurrentHashMap<>();

	private WebSocketSessionHolder() {
	}

	public static void addSession(Object sessionKey, WebSocketSession session) {
		// 修改为支持多会话的添加方式
		USER_SESSION_MAP.computeIfAbsent(sessionKey.toString(),
				k -> new CopyOnWriteArraySet<>()).add(session);
	}

	public static void removeSession(Object sessionKey) {
		// 移除时不再自动清除所有会话
		Set<WebSocketSession> sessions = USER_SESSION_MAP.get(sessionKey.toString());
		if (sessions != null) {
			sessions.removeIf(s -> !s.isOpen()); // 只移除已关闭的连接
			if (sessions.isEmpty()) {
				USER_SESSION_MAP.remove(sessionKey.toString());
			}
		}
	}

	// 保持原有方法签名不变(兼容现有调用)
	public static WebSocketSession getSession(Object sessionKey) {
		Set<WebSocketSession> sessions = USER_SESSION_MAP.get(sessionKey.toString());
		return sessions != null ? sessions.stream().findFirst().orElse(null) : null;
	}

	// 新增方法:获取用户的所有会话
	public static Set<WebSocketSession> getSessions(Object sessionKey) {
		return USER_SESSION_MAP.getOrDefault(sessionKey.toString(), Collections.emptySet());
	}

	// 保持原有方法不变
	public static Collection<WebSocketSession> getSessions() {
		List<WebSocketSession> allSessions = new ArrayList<>();
		USER_SESSION_MAP.values().forEach(allSessions::addAll);
		return allSessions;
	}

	// 保持原有方法不变
	public static Set<String> getSessionKeys() {
		return USER_SESSION_MAP.keySet();
	}
}
相关推荐
比奥利奥还傲.4 分钟前
哪吒监控面板实战:部署 Dashboard、接入 Agent、钉钉告警,再配置固定公网访问
网络·人工智能·分布式·1024程序员节
Mr YiRan5 小时前
网络请求API监控与网络切换埋点
android·网络
Shota Kishi6 小时前
Solana Shreds 的 UDP 直投架构:Shredstream UDP Forwarding 的设计与实现
网络协议·架构·udp·区块链·solana
李昊哲小课8 小时前
Spring Boot SSE 实时推送教程
spring boot·websocket·flux·sseemitter
当下新鲜事12 小时前
车间实测记录:三菱电机MR-J5伺服与压力控制方案的场景适配
网络·物联网·业界资讯
门思科技12 小时前
开源网关有哪些:主流类型梳理
网络·python·物联网
szephyr13 小时前
WebSocket 实战:心跳、断线重连、鉴权,一次讲清
前端·websocket·node.js·长连接·实时通信
llilian_1614 小时前
北斗授时卡同步解决方案 gnss授时卡 计算机时间同步板卡
大数据·网络·人工智能·功能测试·51单片机
Vicky_time14 小时前
跨境物流系统架构:美国海外仓与FBA中转海外仓选型技术方案评测
网络·人工智能
玄芯散人14 小时前
【筑基·055】TCP vs UDP:可靠和快速的取舍
网络协议·tcpdump