我正在关注本教程:https://www.baeldung.com/websockets-spring
我测试了该应用程序,它在嵌入式 tomcat 服务器上运行时运行良好。但是,当我尝试在外部 tomcat 服务器上部署并运行相同的应用程序时,它会崩溃,因为 URL 不是
localhost:8080/chat
变成了
myhostIP:port/spring-boot-web-jsp/chat
所以我修改了 javascript 文件,在现有 URL 前面添加了/spring-boot-web-jsp 。当我运行 web 应用程序时,套接字连接成功并发送数据。但是现在我的 Spring MVC Controller 无法工作。
我的 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('/spring-boot-web-jsp-1.0/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/spring-boot-web-jsp-1.0/topic/messages', function(messageOutput) {
showMessageOutput(JSON.parse(messageOutput.body));
});
});
}
function disconnect() {
if(stompClient != null) {
stompClient.disconnect();
}
setConnected(false);
console.log("Disconnected");
}
function sendMessage() {
var from = document.getElementById('from').value;
var text = document.getElementById('text').value;
stompClient.send("/spring-boot-web-jsp-1.0/app/chat", {},
JSON.stringify({'from':from, 'text':text}));
}
function showMessageOutput(messageOutput) {
var response = document.getElementById('response');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(messageOutput.from + ": "
+ messageOutput.text + " (" + messageOutput.time + ")"));
response.appendChild(p);
}
我的 Controller :
@MessageMapping("/chat")
@SendTo("/topic/messages")
public OutputMessage send(Message message) throws Exception {
String time = new SimpleDateFormat("HH:mm").format(new Date());
return new OutputMessage(message.getFrom(), message.getText(), time);
}
我的消息代理:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat");
registry.addEndpoint("/chat").withSockJS();
}
}
我尝试将 Controller 修改为:
@MessageMapping("app/chat")
@SendTo("/topic/messages")
public OutputMessage send(Message message) throws Exception {
String time = new SimpleDateFormat("HH:mm").format(new Date());
return new OutputMessage(message.getFrom(), message.getText(), time);
}
@MessageMapping("spring-boot-web-jsp-1.0/app/chat")
@SendTo("spring-boot-web-jsp-1.0/topic/messages")
public OutputMessage send(Message message) throws Exception {
String time = new SimpleDateFormat("HH:mm").format(new Date());
return new OutputMessage(message.getFrom(), message.getText(), time);
}
以及许多其他变体,但它们都不起作用。 在通过外部 Apache Tomcat 以及嵌入式(设置某种相对 URL)进行测试时,如何修改 Controller 和 javascript 文件以使其工作?我怎样才能让它在外部 Tomcat 上正常工作?
最佳答案
- 删除 tomcat/webapps/ROOT 目录
- 将最终的 jar/war/ear 文件重命名为 ROOT.jar/war/ear
- 部署到tomcat
Tomcat 会将您的应用程序部署在根目录 localhost:8080/chat 下
关于javascript - 设置javascript的相对URL(websocket连接到Spring MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58112826/