【发布时间】:2014-08-22 05:59:40
【问题描述】:
我正在使用 Spring Boot 和 Tomcat 7 使用 STOMP 和 sockJS 创建一个带有 Websockets 的 Web 应用程序。以下是我的请求映射的类:
@Controller
public class HelloController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String index() {
return "index";
}
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
return new Greeting("Hello, " + message.getName() + "!");
}
}
很遗憾,我在网页上的控制台中收到以下错误:
Opening Web Socket...
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/WarTest/hello/info
Whoops! Lost connection to undefined
Opening Web Socket...
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/WarTest/hello/info
Whoops! Lost connection to undefined
你好是我的大部分代码包,但我不确定导致错误的“信息”目录是什么。有没有人可以帮助我?
编辑: 这是我的 Javascript 客户端代码:
function connect() {
var socket = new SockJS('/WarTest/hello');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function(greeting){
showGreeting(JSON.parse(greeting.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);
还有我的 websocketconfig:
@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("/hello").withSockJS();
}
}
编辑:
WebInitializer.java
public class WebInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
EDIT2:
WebsocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.setApplicationDestinationPrefixes("/app").enableSimpleBroker("/queue","/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}
EDIT3:
当我使用 SockJS('http://localhost:8080/WarTest/hello'): 直接从 IDE 启动它时,我得到了这个
Opening Web Socket... stomp.js:130
Web Socket Opened... stomp.js:130
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
stomp.js:130
<<< CONNECTED
heart-beat:0,0
version:1.1
stomp.js:130
connected to server undefined stomp.js:130
Connected: CONNECTED
version:1.1
heart-beat:0,0
(index):23
>>> SUBSCRIBE
id:sub-0
destination:/topic/greetings
但是,当我作为 WAR 部署到 Tomcat 时,我收到了找不到 URL/信息的错误。
【问题讨论】:
-
您应该显示用于连接的端点配置和客户端代码。看起来您使用错误的
URL连接到 WebSocket 端点 -
感谢 Artem 的及时回复!我添加了一些我的实现。让我知道这是否有帮助,或者我还有什么可以补充的!
-
请显示,您如何注册
DispatcherServlet?或者您只是想这样做,但再显示一次WebSocketConfig... -
抱歉,搞混了。更新了 WebsocketConfig 和 WebInitializer
-
您能否显示用于访问带有 WebSocket 客户端代码的页面的 URL?那是来自同一个 WAR 的页面吗?
标签: tomcat spring-boot spring-websocket