当前位置:   article > 正文

副本技能-使用RabbitMQ做即时消息通讯,使用STOMP协议_rabbitmq stomp

rabbitmq stomp

1.消息通讯的基础环境

技术使用:

  • 后端使用Spring Websocket通讯,
  • 前端使用SocketJS,非长连接,有心跳检测
  • 信息交互使用RabbitMQ的插件Stomp

RabbitMQ安装Stomp的插件

安装前:
在这里插入图片描述
安装后:
在这里插入图片描述
安装步骤(我的RabbitMQ是使用的Docker)

  • 1.进入Docker容器RabbitMQ的后台(docker exec -it 容器ID /bin/bash)
  • 2.查看RabbitMQ的插件启用情况(rabbitmq-plugins list)
  • 3.启用RabbitMQ插件(rabbitmq_stomp, rabbitmq_web_stomp)
  • 4.关闭RabbitMQ服务
  • 5.修改Docker的端口配置文件(hostconfig.json,config.v2.json)路径:/www/server/docker/containers/容器ID
  • 6.重启Docker服务,放行服务端口
  • 7.启动本地服务,可以链接成功

WebSocket配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    private final WebSocketProperties webSocketProperties;

    public WebSocketConfig(WebSocketProperties webSocketProperties) {
        this.webSocketProperties = webSocketProperties;
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.setApplicationDestinationPrefixes("/ws")
                .enableStompBrokerRelay("/topic")
                // rabbitmq 必须开启中继插件
                .setRelayHost(webSocketProperties.getHost())
                .setRelayPort(webSocketProperties.getStompPort())
                .setClientLogin(webSocketProperties.getUsername())
                .setClientPasscode(webSocketProperties.getPassword())
                .setSystemLogin(webSocketProperties.getUsername())
                .setSystemPasscode(webSocketProperties.getPassword())
                .setVirtualHost(webSocketProperties.getVirtualHost());
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint(SERVER_ENDPOINTS)
                .setHandshakeHandler(new UserHandshakeHandler())
                // 如果走网关,请检查网关的跨域,可能依旧有跨域
                .setAllowedOrigins("*")
                .withSockJS();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

前端代码

let gateWay = "xxx";
let token = "xxx";
// 此处路径为后端的服务端点
let socket = new SockJS(gateWay + "/center-message-service/center/msg/notification?token=" + token);
let headers = {
    "authorization":"Bearer " + token
}
stompClient = Stomp.over(socket);
stompClient.connect(headers, function (frame) {
    console.log("Connected: " + frame)
    updateNotificationDisplay();
    updateOnlineUserDisplay();
    // stomp订阅的路径不能用/,前缀/topic是前后端约定
    stompClient.subscribe('/topic/xxxx', function (message) {
        console.info(message);
        console.info("<<<<");
    });
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/484216
推荐阅读
相关标签
  

闽ICP备14008679号