简体   繁体   English

Spring Websocket无法到达控制器

[英]Spring Websocket Not Reaching Controller

I am trying to implement this websocket example but currently the requests are not being routed to my Spring Controllers. 我正在尝试实现此websocket示例,但是当前请求未路由到我的Spring Controllers。

http://spring.io/guides/gs/messaging-stomp-websocket/ http://spring.io/guides/gs/messaging-stomp-websocket/

Below is my WebSocketConfig class: 下面是我的WebSocketConfig类:

package com.kinnect.mobile.gateway.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@ComponentScan(basePackages = {"com.kinnect.mobile.gateway.controller"})
@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();
    }

}

Here is my controller: 这是我的控制器:

package com.kinnect.mobile.gateway.controller;

import hello.Greeting;
import hello.HelloMessage;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

    @Controller
    public class GreetingController {

        @MessageMapping("/hello")
        @SendTo("/topic/greetings")
        public Greeting greeting(HelloMessage message) throws Exception {
            System.out.println("Test...");
            return new Greeting("Hello, " + message.getName() + "!");
        }

    }

Here is my JavaScript used to call the controller: 这是我用来调用控制器的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('http://localhost:8080/KinnectMobileGateway/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('http://localhost:8080/KinnectMobileGateway/topic/greetings', function(greeting){
                console.log("Greeting received!");
                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("http://localhost:8080/KinnectMobileGateway/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);
    }

I am able to make the socket connection and I can see this in the console on the server when I attempt to make a socket connection, but the System.out.println("Test") does not get triggered: 我能够建立套接字连接,并且在尝试建立套接字连接时可以在服务器的控制台中看到此连接,但不会触发System.out.println(“ Test”):

16:13:47.044 [http-bio-8080-exec-2] DEBUG o.s.m.simp.stomp.StompDecoder - Decoded [Payload byte[14]][Headers={stompCommand=SEND, nativeHeaders={content-length=[14], destination=[http://localhost:8080/KinnectMobileGateway/app/hello]}, simpMessageType=MESSAGE, simpDestination=http://localhost:8080/KinnectMobileGateway/app/hello, id=b49ca021-a789-9fdc-66cd-413d7549fb90, timestamp=1400573627044}]

I think the config is wrong somehow and my controller class is not getting registered at startup but I am not sure how. 我认为配置某种程度上是错误的,并且我的控制器类没有在启动时注册,但是我不确定如何。 Please help! 请帮忙!

Try this: 尝试这个:

stompClient.subscribe('/topic/greetings'
...

stompClient.send("/app/hello"

You should use relative paths for your logic. 您应该为逻辑使用relative paths The StackTrace says you correct info: StackTrace说您更正了以下信息:

simpDestination=http://localhost:8080/KinnectMobileGateway/app/hello 

But your destination is /app/hello 但是您的目的地是/app/hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM