LonglyDean 2019-04-14 16:09 采纳率: 0%
浏览 1862

Springboot+vue+sockjs 连不上服务器

1.问题困扰我好几天了。我用vue做前后分离去连接Springboot后台

//这是SpringBoot后台

public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{


    @Autowired
    private SimpMessagingTemplate template;

    //广播推送消息
    @Scheduled(fixedRate = 10000)
    public void sendTopicMessage() {
    System.out.println("后台广播推送!");
    User user=new User();
    user.setUserName("oyzc");
    user.setAge(10);
        this.template.convertAndSend("/topic/getResponse",user);
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/endpoint-websocket").setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic","/chat");
        registry.setApplicationDestinationPrefixes("/app");
    }

}

用的是Springboot整合的websocket

//这是vue,JS页面
<template>
  <div >

  </div>
</template>

<script>

  import SockJS from  'sockjs-client';
  import  Stomp from 'stompjs';
  export default {
    data(){
      return {
        stompClient:'',
        timer:'',
        msg:'SocketJS'
      }
    },
    methods:{
      connection(){
        let socket = new SockJS('http://localhost:8080/endpoint-websocket');
        debugger;
        this.stompClient = Stomp.over(socket);
        console.log('stompClient:' + this.stompClient);
        this.stompClient.connect({}, function(frame){
          console.log('Connected:' + frame);
          this.stompClient.subscribe('/topic/getResponse', (result) => {
            console.info(result)
            showContent(Json.parse(result.body));
          });
        });
      },


      disconnect() {
        if (this.stompClient) {
          this.stompClient.disconnect();
        }
      },  // 断开连接
    },

    mounted(){
      this.connection();
    },
    beforeDestroy: function () {
      // 页面离开时断开连接,清除定时器
      this.disconnect();

    }
  }
</script>

<style scoped>

</style>

以下是浏览器报错信息
图片说明

图片说明

  • 写回答

1条回答 默认 最新

  • m0_46198732 2020-04-08 18:38
    关注

    var socket = new SockJS('/endpoint-websocket'); 这样就可以了

    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决