您的位置:首页 > 科技 > 能源 > 公司网页制作免费_国内有实力的软件开发公司_广东今天新闻最新消息_龙岗网站推广

公司网页制作免费_国内有实力的软件开发公司_广东今天新闻最新消息_龙岗网站推广

2024/11/18 1:33:58 来源:https://blog.csdn.net/wzs535131/article/details/142629794  浏览:    关键词:公司网页制作免费_国内有实力的软件开发公司_广东今天新闻最新消息_龙岗网站推广
公司网页制作免费_国内有实力的软件开发公司_广东今天新闻最新消息_龙岗网站推广

1.项目背景 :使用websocket 和前端通信但是后端是分布式部署,session 无法共享,并且websocketSession 无法序列化,所以无法把session存到redis 中。

2.基础代码:

@Configuration
@Slf4j
public class RedisConfig {@Beanpublic RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new PatternTopic(Constants.REDIS_CHANNEL));return container;}@Beanpublic MessageListenerAdapter listenerAdapter(RedisReceiver receiver) {return new MessageListenerAdapter(receiver, "onMessage");}}
@Slf4j
@Component
public class RedisReceiver implements MessageListener {@Autowiredprivate TestHandler testHandler;@Overridepublic void onMessage(Message message, byte[] pattern) {try {log.info("RedisReceiver onMessage:{}",new String(message.getBody(), StandardCharsets.UTF_8));String channel = new String(message.getChannel());String content = new String(message.getBody(), StandardCharsets.UTF_8);if (Constants.REDIS_CHANNEL.endsWith(channel)) {testHandler.sendMessageToClient(content);}} catch (Exception e) {log.error("RedisReceiver 消息处理失败",e);}}}
@Slf4j
@Component
public class TestHandler extends TextWebSocketHandler {private static final ConcurrentHashMap<String, WebSocketSession> SESSION_DISPATCH_HOLDER = new ConcurrentHashMap<>();@Resourceprivate StringRedisTemplate stringRedisTemplate;@Overridepublic void afterConnectionEstablished(@NonNull WebSocketSession session) throws Exception {super.afterConnectionEstablished(session);SESSION_DISPATCH_HOLDER.put(session.getId(), session);log.info("webSocket already connected:{}",session.getId());}@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {super.afterConnectionClosed(session, status);SESSION_DISPATCH_HOLDER.remove(session.getId());log.info("webSocket disConnect:{}",session.getId());}@Overridepublic void handleMessage(WebSocketSession session, WebSocketMessage<?> message) {log.info("handleMessage enter message:{}", JsonUtils.objToJsonStr(message.getPayload()));TextMessage textMessage = new TextMessage("");try {session.sendMessage(textMessage);} catch (IOException e) {log.error("发送消息失败", e);}}public void sendMessageToClient(String contentList) {log.info("sendMessageToClient:{}", contentList);if (CollectionUtils.isEmpty(SESSION_DISPATCH_HOLDER)) {log.info("没有连接中的客户端");return;}SESSION_DISPATCH_HOLDER.values().parallelStream().forEach(session -> {try {TextMessage textMessage = new TextMessage(contentList);session.sendMessage(textMessage);log.info("websocket 消息推送成功:{}", contentList);} catch (Exception e) {log.error("发送消息失败", e);}});}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com