Spring WebSocket - 通知订阅者不起作用

时间:2018-04-06 09:14:45

标签: spring spring-mvc websocket spring-websocket spring-4

我正在尝试让Spring Websockets(Spring 4)在我正在开发的项目中工作。到目前为止,我已经打开Websocket,添加订阅者并发送消息。

从昨天开始,我一直试图弄清楚为什么我的端点不会处理我的HTML中包含的stomp客户端发送的消息。控制台中实际上没有错误(好吧,我不是100%确定“连接到服务器未定义”这意味着什么)。

以下是Chrome控制台的输出:

Opening Web Socket...
Web Socket Opened...

>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:xxx@yyy.com

connected to server undefined

Connected: CONNECTED
user-name:xxx@yyy.com
heart-beat:0,0
version:1.1


>>> SUBSCRIBE
id:sub-0
destination:/my-project/notify-open-documents/1

Sending message : {"documentId":"1"}

>>> SEND
destination:/my-project/ws/lock-document
content-length:19

{"documentId":"1"}

,这是我的代码:

配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/notify-open-documents");
        registry.setApplicationDestinationPrefixes("/ws");   
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry.addEndpoint("/lock-document");
        stompEndpointRegistry.addEndpoint("/lock-document").withSockJS();
    }
}

控制器:

@Controller
public class DocumentWebsocketController {

    @MessageMapping("/lock-document")
    @SendTo("/notify-open-documents/{id}")
    public Response response(@DestinationVariable("id") Long id, Message message) {
        return new Response(message.getDocumentId());
    }
}

和我的HTML的STOMP相关部分:

<script type="text/javascript">

    $(document).ready(function() { 
        connect(); 
    });

    var stompClient = null;

    function connect() {
        var socket = new SockJS('<spring:url value="/lock-document"/>');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function (frame) {
            console.log('Connected: ' + frame);
            stompClient.subscribe('<spring:url value="/notify-open-documents/${id}"/>', function (messageOutput) {
                console.log('Receiving message: ' + JSON.parse(messageOutput.body));
            });
            sendName();
        });
    }

    function disconnect() {
        if (stompClient !== null) {
            stompClient.disconnect();
        }
        console.log("Disconnected");
    }

    function sendName() {
        console.log("Sending message : " + JSON.stringify({'documentId' : "${id}" }));
        stompClient.send('<spring:url value="/ws/lock-document"/>', {}, JSON.stringify({'documentId': "${id}"}));
    }

</script>

我真的没有想法可能出错了,我的浏览器支持WebSockets,从我在日志中看到的,WebSocket正在正常打开,消息正在发送,但我无法弄清楚的是为什么我的Controller无法处理传入的消息。

1 个答案:

答案 0 :(得分:2)

在HTML代码段中,我看到您正在使用spring:url标记生成STOMP目标。 spring:url标记添加了对STOMP目标没有意义的上下文路径。

您正在配置的应用程序目标前缀是/ws和代理目标前缀/notify-open-documents,这些都不会匹配您的上下文路径/my-project(来自您的控制台输出)。从订阅目标中删除上下文路径,并在发送消息时(不是从SockJS URL):

stompClient.send('/ws/lock-document', {}, JSON.stringify({'documentId': "${id}"}));