基于WebSocket + STOMP + SockJS + RabbitMq的聊天室Demo

目录

一、简介

老早之前写的一个基于WebSocket + STOMP + SockJS + RabbitMq的聊天室Demo,感兴趣的小伙伴可以鼓捣一下。

  • 相关技术:WebSocket + STOMP + SockJS + RabbitMq
  • 核心功能:支持广播消息、给指定用户发消息
  • 后端支持集群部署,且客户端到后端需支持会话保持

聊天室页面展示

Cluster Support

具体代码参见:
https://gitee.com/luoex/spring-boot-websocket-chat-demo

二、RabbitMq启用STOMP

bash 复制代码
# linux 
rabbitmq-plugins enable  rabbitmq_stomp

# windows(执行完如下命令后需重启RabbitMQ服务)
rabbitmq-plugins.bat enable rabbitmq_stomp

windows安装结果

bash 复制代码
D:\programs\RabbitMq3.9.11\rabbitmq_server-3.9.11\sbin>rabbitmq-plugins.bat enable rabbitmq_stomp
Enabling plugins on node rabbit@TSD-LUOHQ:
rabbitmq_stomp
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_stomp
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@TSD-LUOHQ...
The following plugins have been enabled:
  rabbitmq_stomp

set 4 plugins.
Offline change; changes will take effect at broker restart.

三、相关Destination说明

destionation分为3种类型:

  • 广播 - /topic前缀,向所有订阅此类destination的客户端发布消息
  • 单播 - /user前缀,仅向所有订阅此类destination的指定用户的客户端发送消息(仅客户端使用/user前缀,后端无需使用/user前缀)
  • 应用处理 - /app前缀,可有客户端向此类destination发布消息,然后由后端@Controller类中的@MessageMapping方法接收消息并处理(仅客户端使用/app前缀,后端无需使用/app前缀)
类型 前缀 类型说明 前缀使用说明
广播 /topic 向所有订阅此类destination的客户端发布消息, 例如:/topic/public 前后端均需使用/topic前缀
单播 /user 仅向所有订阅此类destination的指定用户的客户端发送消息, 例如:/user/msg 仅客户端使用/user前缀,后端无需使用/user前缀
应用处理 /app 可有客户端向此类destination发布消息,然后由后端@Controller类中的@MessageMapping方法接收消息并处理, 例如:/app/chat.sendMessage 仅客户端使用/app前缀,后端无需使用/app前缀

客户端相关destination:

destination 关联方式 说明
/topic/public 订阅 接收广播消息
/user/topic/msg 订阅 接收个人专属消息
/app/chat.addUser 发布 发布当前用户上线消息
/app/chat.sendMessage 发布 发布聊天消息

服务端相关destination:

destination 关联方式 说明
/topic/public 发布 发布广播消息
/topic/msg 发布 发布个人专属消息
/chat.addUser 订阅 @MessageMapping("/chat.addUser") 接收当前用户上线消息
/chat.sendMessage 订阅 @MessageMapping("/chat.sendMessage") 接收聊天消息
相关推荐
大飞哥~BigFei14 分钟前
RabbitMq消费消息遇到的坑
java·rabbitmq·java-rabbitmq
赖small强37 分钟前
【ZeroRange WebRTC】Kinesis Video Streams WebRTC Data Plane WebSocket API 深度解析
websocket·webrtc·sdp·offer/answer·master/viewer
刘逸潇20051 小时前
Socket:TCP/UDP通信详解
python·websocket·udp·tcp
初听于你1 小时前
深入解析IP, ICMP, OSPF, BGP四大核心网络协议
服务器·网络·网络协议·计算机网络·信息与通信·信号处理
百***37481 小时前
Spring Boot 中 RabbitMQ 的使用
spring boot·rabbitmq·java-rabbitmq
yumo_fly1 小时前
欧拉22.03系统安装RabbitMQ-3.6.10
分布式·rabbitmq·ruby
百***46801 小时前
RabbitMQ之交换机
分布式·rabbitmq·ruby
KYumii1 小时前
RabbitMQ应用(2)
分布式·rabbitmq
百***92021 小时前
RabbitMQ 客户端 连接、发送、接收处理消息
分布式·rabbitmq·ruby