4

Spring Boot と Tomcat 7 を使用して、STOMP と sockJS を使用して Websocket で 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() + "!");
    }
}

残念ながら、Web ページのコンソールに次のエラーが表示されます。

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

hello はほとんどのコードを含む私のパッケージですが、エラーの原因となっている「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:

を使用して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コンストラクターに追加する必要があるようです。春のリファレンスマニュアルから:

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.xmlまたは) に登録されている に基づいています。WebApplicationInitializer例えば/spring

  3. 各サービス ( Controller、そのメソッド、または WebSocket エンドポイント) は、何らかの URI パスの下に登録されます。あなたの場合はです/hello

したがって、完全な SockJS URL は次のようになります。

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

portただし、正しく指定すると。

于 2014-07-01T16:00:38.467 に答える