无法使用Websocket应用程序加载资源

时间:2014-07-01 15:38:28

标签: tomcat spring-boot spring-websocket

我使用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

你好,我的包装中包含大部分代码,但我不确定" info"导致错误的目录是。是否有人可以帮助我?

编辑: 这是我的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 / info的错误。

1 个答案:

答案 0 :(得分:2)

看起来您应该将应用程序上下文添加到SockJS构造函数中。来自Spring参考手册:

var socket = new SockJS("/spring-websocket-portfolio/portfolio");

配置为:

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

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/portfolio").withSockJS();
    }

但是,WebApp部署在上下文/spring-websocket-portfolio下。

<强>更新

如果您显示网络应用的网址,那就太棒了。假设您应该使用来自SockJS构造函数的WebSocket端点的完整 URL:

var socket = new SockJS("http://localhost:8080/WarTest/hello");

<强> UPDATE2

你应该做几件事:

  1. 在某些情况下部署了webapp。我想,在你的情况下它是WarTest。简而言之,是没有.war扩展名的WAR名称。

  2. Spring MVC应用程序基于DispatcherServlet,它在webapp配置(web.xmlWebApplicationInitializer)中注册了一些映射。例如。 /spring

  3. 每个服务(Controller,其方法或WebSocket端点)都在某个URI路径下注册。在您的情况下,它是/hello

  4. 所以,你完整的SockJS URL应该是这样的:

    http://localhost:8080/WarTest/spring/hello
    

    但是,如果您正确指定了port