首页 新闻 会员 周边 捐助

websocket springboot 跨源问题求助,太折磨了

0
悬赏园豆:200 [待解决问题]

最近初学websocket,一直出现这个问题搜了几天网上类似的问题都没解决,求助。
跨域访问配置
@Configuration
public class CorsConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
            .allowedOriginPatterns("*")
            .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
            .allowCredentials(true)
            //.maxAge(3600)
            .allowedHeaders("*").exposedHeaders("Access-Control-Allow-Headers", "Access-Control-Allow-Methods", "Access-Control-Allow-Origin",
                    "Access-Control-Max-Age", "Access-Control-Request-Headers",
                    "X-Frame-Options");
}

}
配置WebSocket

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

    registry.addEndpoint("/testcenter").setAllowedOrigins("*").withSockJS();

}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
   
    registry.enableSimpleBroker("/topic");
  
}
@Bean
public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
}

}

WSController
@RestController

@CrossOrigin
@RequestMapping("/devopsinfo")
public class WSController {
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;

@GetMapping("/pipeline")

public String pipeline(@RequestParam("name") String name){
    simpMessagingTemplate.convertAndSend("/topic/getResponse",name);
    return "{\"msg\":\"success\",\"code\":\"200\"}";
}

}

html文件

<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<!-- <meta http-equiv="Access-Control-Allow-Origin" content="">-->
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">-->
<link rel="shortcut icon" href="#" />
<!-- <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />-->
<title>Title</title>
<!-- <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>-->
<!-- <script src="https://cdn.bootcss.com/sockjs-client/1.4.0/sockjs.min.js"></script>-->
<!-- <script src="https://cdn.bootcss.com/stomp.js/2.3.2/stomp.min.js"></script>-->
<script type="javascript" src="jquery-3.4.1.js" charset="utf-8" ></script>
<script type="text/javascript" src="sockjs.js" charset="utf-8" ></script>
<script type="text/javascript" src="stomp.js" charset="utf-8 "></script>
</head>
<body>
<div id="msg"></div>
<script>
// import SockJS from 'sockjs-client';
//import Stomp from '/stompjs';
var socket = new SockJS("127.0.0.1/:8080/testcenter");
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
//setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/getResponse', function(response){
// response.setHeader("Access-Control-Allow-Origin", "
");
// response.addHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
// response.addHeader("Access-Control-Allow-Credentials", "true");
// response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
console.log(response.body);
$("#msg").prepend("<div style="border-top:1px solid #000">" +
response.body+"<br/>" +
"</div>")
});
});
</script>
</body>

</html>

打开html浏览器显示:Opening Web Socket...
127.0.0.1:8080/testcenter/info?t=1641234991529:1 Failed to load resource: the server responded with a status of 500 ()
stomp.js:144 Whoops! Lost connection to http://127.0.0.1:8080/testcenter

springboot2.6.2看有说是tomcat版本问题,但最新的应该没这些问题啊。。。

ipromise3的主页 ipromise3 | 初学一级 | 园豆:2
提问于:2022-01-04 09:31
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册