修改 blade-demo模块
pom文件增加依赖
org.springframework.boot spring-boot-starter-websocket
增加WebSocketAutoConfig 文件
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker public class WebSocketAutoConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/bullet","/websocket") //开启/bullet端点 .setAllowedOrigins("*") //允许跨域访问 .withSockJS(); //使用sockJS } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { //订阅Broker名称 //registry.enableSimpleBroker("/topic","/user"); registry.enableSimpleBroker("/toAll","/topic"); //全局使用的订阅前缀(客户端订阅路径上会体现出来) //registry.setApplicationDestinationPrefixes("/app/"); // //点对点使用的订阅前缀(客户端订阅路径上会体现出来),不设置的话,默认也是/user/ //registry.setUserDestinationPrefix("/user/"); } }
增加BulletController
package com.example.demo.controller; import com.example.demo.dto.BulletMessageDTO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class BulletController { private static final Logger logger=LoggerFactory.getLogger(BulletController.class); @MessageMapping("/hellowebsocket") //SendTo 发送至 Broker 下的指定订阅路径 @SendTo("/topic/helloworld") // @SendTo("/toAlltoAll/bulletScreen") public String say(BulletMessageDTO clientMessage) { //方法用于广播测试 if (clientMessage!=null){ if (clientMessage.getMessage()!=null){ clientMessage.setMessage(clientMessage.getMessage().trim()); } } logger.info(clientMessage.getUsername()+":"+clientMessage.getMessage()); return clientMessage.getMessage(); } //注入SimpMessagingTemplate 用于点对点消息发送 @Autowired private SimpMessagingTemplate messagingTemplate; @RequestMapping("/send") public void send(String msg){ messagingTemplate.convertAndSend("/topic/helloworld", msg + "hello!!!"); } }
增加blade-gateway 网关配置文件
[ { "id": "websocket_sockjs_route", "order": -1, "predicates": [ { "name": "Path", "args": { "pattern": "/websocket/info/**" } } ], "filters": [], "uri": "lb://blade-demo" }, { "id": "webSocketRoute", "order": -1, "predicates": [ { "name": "Path", "args": { "pattern": "/websocket/**" } } ], "filters": [], "uri": "lb:ws://blade-demo" } ]
新增静态页面 index.html 192.168.7.182 网关地址
Spring Boot WebSocket+广播式
提示错误:
1、老是提示跨域问题,网关都是自行处理过了
2、blade-demo 模块提示错误日志 /info 404
扫一扫访问 Blade技术社区 移动端