Java WebSocket握手-意外响应代码200-AngularJs和Spring引导
当我试图在AngularJS应用程序和Spring Boot之间建立websocket通信时,我得到了一个错误:websocket握手时出错-意外响应代码:200 以下是我的JS代码:Java WebSocket握手-意外响应代码200-AngularJs和Spring引导,java,angularjs,spring,websocket,Java,Angularjs,Spring,Websocket,当我试图在AngularJS应用程序和Spring Boot之间建立websocket通信时,我得到了一个错误:websocket握手时出错-意外响应代码:200 以下是我的JS代码: function run(stateHandler, translationHandler, $websocket) { stateHandler.initialize(); translationHandler.initialize(); var ws = $websocket.$ne
function run(stateHandler, translationHandler, $websocket) {
stateHandler.initialize();
translationHandler.initialize();
var ws = $websocket.$new('ws://localhost:8080/socket'); // instance of ngWebsocket, handled by $websocket service
ws.$on('$open', function () {
console.log('Oh my gosh, websocket is really open! Fukken awesome!');
});
ws.$on('/topic/notification', function (data) {
console.log('The websocket server has sent the following data:');
console.log(data);
ws.$close();
});
ws.$on('$close', function () {
console.log('Noooooooooou, I want to have more fun with ngWebsocket, damn it!');
});
}
下面是我的Java代码:
WebsocketConfiguration.java
WebsocketSecurityConfiguration.java
@覆盖
受保护的无效配置入站(MessageSecurityMetadataSourceRegistry消息){
信息
//非消息和订阅的消息类型
.nullDestMatcher().authenticated()
//匹配以/rooms开头的任何目标/
.simpDestMatchers(“/topic/tracker”).hasAuthority(AuthoritiesConstants.ADMIN)
.simpDestMatchers(“/topic/**”).permitAll()
//(即不能直接向/topic/,/queue/发送消息)
//(即,无法订阅/topic/messages/*以获取发送到的邮件。)
///topic/messages用户)
.simpTypeMatchers(SimpMessageType.MESSAGE,SimpMessageType.SUBSCRIBE).denyAll()
//一网打尽
.anyMessage().denyAll();
}
有人知道如何解决这个问题吗?提前谢谢你 我有一个类似的问题,我正在使用chrome插件(简单websocket客户端)测试我的websocket连接,并试图连接到ws://localhost:8080/handler/,它在我的代码中定义为
registry.addEndpoint(“/handler”).setAllowedOrigins(*”).withSockJS()代码>但发生意外错误200。我已通过在chrome扩展名上的客户端请求字符串中添加/websocket修复了此问题,因此您可以尝试在JS文件中更改以下行:
var ws = $websocket.$new('ws://localhost:8080/socket');
到
我不知道为什么在我的情况下,我只是偶然发现了它,如果有人能进一步澄清它,那就太好了:)你能试试这个WebSocket配置吗:
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*");
stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
}
所以您既有websocket配置,也有SockJS配置?如果您将SockJS客户端配置为在websocket端点上使用SockJS,则需要使用它。我发现使用.withSockJS()配置spring;表示必须添加“websocket”。我想是因为你提供了一个退路?如果删除.withSockJS();您不需要附加websocket。谢谢你的回答,顺便说一句!谢谢,你救了我一个晚上!文档中是否显示了这一点?我什么也找不到……这是正确的答案。使用with sockjs()时,必须将/websocket添加到url中。如果没有SockJS,你必须使用WebSocketConfiguration中的url。这对我来说也很有效,但却让我发疯,因为我无法在代码中找到它工作的原因。我正在浏览所有内容,但这似乎不仅仅是一个正确的解决方案,更像是一个黑客。@PeterCatalin您可以在link>中检查后缀/websocket,我认为您的代码将只使用端点的第一个配置。当浏览器中没有websocket时,.withSockJS()应该将端点配置为回退到SockJS。但是,如果您使用.withSockJS(),那么您似乎还需要对web客户端进行设置以使用它,否则您将在协商期间获得200状态。比较:与。
var ws = $websocket.$new('ws://localhost:8080/socket');
var ws = $websocket.$new('ws://localhost:8080/socket/websocket');
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*");
stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
}