4

我正在使用 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);
    }

}

编辑2:

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();
    }

}

编辑3:

当我使用 IDE 直接启动它时,我得到了这个SockJS('http://localhost:8080/WarTest/hello'):

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/信息的错误。

4

1 回答 1

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 是在 context 下部署的/spring-websocket-portfolio

更新

如果您显示您的 web 应用程序的 URL,那就太好了。假设您应该使用构造函数中的 WebSocket 端点的完整URL SockJS

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

更新2

你应该做几件事:

  1. webapp 是在某些上下文中部署的。我想,在你的情况下是WarTest. .war通常是不带扩展名的 WAR 名称。

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

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

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

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

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

于 2014-07-01T16:00:38.467 回答