赞
踩
技术使用:
安装前:
安装后:
安装步骤(我的RabbitMQ是使用的Docker)
@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(); } }
前端代码
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("<<<<"); }); })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。