集成websocket、gateway时老是提示访问地址404,麻烦帮忙查看

2019-07-18 14:23发布

修改 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+广播式


    
    
        
            连接             断开连接         
                                   
                          发送             

        
    
                            var stompClient = null;         var host="http://192.168.7.182/blade-demo/websocket";         function setConnected(connected) {             document.getElementById('connect').disabled = connected;             document.getElementById('disconnect').disabled = !connected;             document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';             $('#response').html();         }         function connect() {             var socket = new SockJS(host);             stompClient = Stomp.over(socket);             stompClient.connect({}, function(frame) {                 setConnected(true);                 console.log('Connected:' + frame);                 stompClient.subscribe('/topic/helloworld', function(response) {                     showResponse(response.body);                 });             });         }         function disconnect() {             if (stompClient != null) {                 stompClient.disconnect();             }             setConnected(false);             console.log("Disconnected");         }         function send() {             var name = $('#name').val();             var message = $('#messgae').val();             stompClient.send("/hellowebsocket", {}, JSON.stringify({username:name,message:message}));         }         function showResponse(message) {             var response = $('#response');             response.html(message);         }     


提示错误:

1、老是提示跨域问题,网关都是自行处理过了

2、blade-demo 模块提示错误日志  /info   404

image.png