gpt4 book ai didi

java - 为什么spring websocket应用程序看不到html并抛出404错误?

转载 作者:行者123 更新时间:2023-12-04 13:50:56 24 4
gpt4 key购买 nike

我正在尝试根据指南使用 spring boot 和 websocket 进行聊天。
简单的配置类:

@Configuration
@EnableWebSocketMessageBroker
public class webSocketConfiguration implements WebSocketMessageBrokerConfigurer {

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

@Override
public void registerStompEndpoints(StompEndpointRegistry registry){
registry.addEndpoint("/chat").withSockJS();
}
}

我的 Controller :
@Controller
public class MessageController {

@MessageMapping("/hello")
@SendTo("/topic/chatting")
public Message messageHandler(Name name) throws Exception{
return new Message("Hello, " + name.getName() + " !");
}
}

消息类:
public class Message {
private String content;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Message(String content) {
this.content = content;
}

public Message() {

}
}

和名称类:
package com.example.webSocketChat.Messages;

public class Name {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Name(String name) {
this.name = name;
}

public Name() {

}
}


实际上是 html 页面本身,它位于 资源/静态 ,虽然由于某种原因没有静态文件夹,虽然我通过Spring Initializr创建了项目:
<!DOCTYPE html>
<html>
<head>
<title>Hello WebSocket</title>
<script src="sockjs-0.3.4.js"></script>
<script src="stomp.js"></script>
<script type="text/javascript">
var stompClient = null;

function setConnected(connected) {
document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected;
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
document.getElementById('response').innerHTML = '';
}

function connect() {
var socket = new SockJS('/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/chatting', function(message){
showGreeting(JSON.parse(message.body).content);
});
});
}

function disconnect() {
stompClient.disconnect();
setConnected(false);
console.log("Disconnected");
}

function sendName() {
var name = document.getElementById('name').value;
stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
}

function showGreeting(message) {
var response = document.getElementById('response');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(message));
response.appendChild(p);
}
</script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being enabled. Please enable
Javascript and reload this page!</h2></noscript>
<div>
<div>
<button id="connect" onclick="connect();">Connect</button>
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
</div>
<div id="conversationDiv">
<label>What is your name?</label><input type="text" id="name" />
<button id="sendName" onclick="sendName();">Send</button>
<p id="response"></p>
</div>
</div>
</body>
</html>
它似乎按照指南正确地完成了所有事情并弄清楚发生了什么,但是如果你在启动时访问地址 http://localhost:8080 它会引发 404 错误,但如果您转到地址 http://localhost:8080/chat 将是消息 欢迎来到 SockJS! .
这里可能有什么错误?我没注意到,我根据 2 个指南做了所有事情,并且代码完全相同。我很乐意提供帮助

最佳答案

好的,那么你能澄清一下问题吗?
这就是页面在根 url 处显示 404 错误但/chat 路由有效的原因吗?
如果您问上述问题,请检查 connect() 下的 HTML 文件。你是 在“/chat”路由打开套接字 .
也在您的 端点位于/chat 下的 Java 代码,因此触发了 SockJS 连接。根 url 不做任何事情。

关于java - 为什么spring websocket应用程序看不到html并抛出404错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69507011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com