首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    WebSocket 集群解决方案!

    代码演示 1.Websocket Server 建立userid和session的绑定关系 @ServerEndpoint("/websocket/{businessType}/{userId}") @Component public class WebSocketServer { /** * 若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 * 注意:allSession 只记录当前机器的 客户端连接,不是所有session连接 */ public static ConcurrentHashMap<String, Session> allSession = new ConcurrentHashMap<>(); @Resource private RedisService redisService; /** * 连接建立成功调用的方法 * * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 */ @OnOpen public void onOpen(@PathParam(value = "businessType") String businessType, @PathParam(value = "userId") String userId, Session session, EndpointConfig config) { if (StringUtils.isEmpty(userId)) { return; } /** * 加入到本地map */ allSession.put(userId, session); } /** * 连接关闭调用的方法 */ @OnClose public void onClose(@PathParam(value = "userId") String userId, Session session) { if (StringUtils.isNotEmpty(userId)) { allSession.remove(userId); } } /** * 发生错误时调用 * * @param * @param */ @OnError public void onError(@PathParam(value = "userId") String userId, Session session, Throwable error) { } /** * 用户id * * @param userId * @param message */ public void sendMessageToOneUser(Integer userId, String message, String msgId) { if (userId == null) { return; } Session session = allSession.get(String.valueOf(userId)); if (session != null) { //所有Websocket Server 根据客户端userid找到对应session, 只有存在userid和session的绑定关系的Websocket Server才发送消息到客户端 session.getAsyncRemote().sendText(message); } else { System.err.println("session为空"); allSession.remove(userId + ""); } } } 2.所有Websocket Server 接收消息并处理 @Component @RequiredArgsConstructor public class CreateOrderConsumer implements BaseConsumer { private final WebSocketServer webSo

    01
    领券