的恩惠到期在3小時內。此問題的答案有資格獲得 150聲望獎勵。 Omegon正在尋找此問題的更詳細答案:
我想了解為什么 SockJS 無法連接
我正在嘗試根據指南使用 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
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 頁面本身,它位于resource/static 中,盡管由于某種原因沒有 static 檔案夾,盡管我通過 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>
It seems to have done everything correctly according to the guide and figured out what was going on, but if you go to the address at startup http://localhost:8080 it throws a 404 error but if you go to the address http://localhost:8080/chat will be the message Welcome to SockJS!.
What could be the mistake here? I can't notice it, I did everything according to 2 guides and there is exactly the same code. I will be glad to help
uj5u.com熱心網友回復:
首先,webjars-locator由于未提及版本,因此在構建專案時存在依賴性問題。
implementation 'org.webjars:webjars-locator:0.32'在build.gradle 中更改為。
接下來將 html 頁面的名稱從app.html 更改為index.html。默認的 html僅被認為是index.html。
就這樣。
uj5u.com熱心網友回復:
好的,那么你能澄清一下問題嗎?這就是頁面在根 url 處顯示 404 錯誤但 /chat 路由有效的原因嗎?
如果您問上述問題,請檢查connect()您在“/chat”路由下打開套接字的 HTML 檔案。同樣在您的Java 代碼中,端點位于 /chat 下,因此會觸發 SockJS 連接。根網址不做任何事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/321965.html
標籤:java spring spring-boot spring-mvc websocket
