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

Blade 未结 1 3608
hwxiang
hwxiang 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

1条回答
  •  admin
    admin (楼主)
    2019-07-20 00:02

    我先记录一下,后期集成成功后把例子传到私服

    0 讨论(1)
提交回复
热议问题
新增异常 2
seata部署 1
boot项目集群部署,可能出现token接口好使,但是请求未授权 1
4.1.0升级版本4.2.0版本升级问题 2
新增按钮,查看按钮,修改按钮都不显示 1
mysql 查出来的数据在控制台打印,这个在哪里可以关 1
Property "vaildData" was accessed during 前端报错 2
node-red里数据读取不完整 2
OSS如何设置文件上传的大小 1
blade-swagger 自动服务发现 1
layui

扫一扫访问 Blade技术社区 移动端