带有sockJS的Spring WebSocket MissingSessionUserException:消息中没有“user”标题

时间:2017-04-14 07:33:20

标签: java spring websocket spring-websocket sockjs

您好我的websocket有问题。从我的客户端应用程序,我可以连接和订阅

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

<<< CONNECTED
version:1.1
heart-beat:0,0

>>> SUBSCRIBE
id:sub-0
destination:/user/queue/notifications

但是当我想向订阅频道发送消息时,我收到来自java的错误

    14602 [clientInboundChannel-7] ERROR org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler [] - Unhandled exception
org.springframework.messaging.simp.annotation.support.MissingSessionUserException: No "user" header in message
    at org.springframework.messaging.simp.annotation.support.PrincipalMethodArgumentResolver.resolveArgument(PrincipalMethodArgumentResolver.java:42) ~[spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) ~[spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:129) ~[spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:102) ~[spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:462) [spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:482) [spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:86) [spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:423) [spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:361) [spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:135) [spring-messaging-4.2.0.RELEASE.jar:4.2.0.RELEASE]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_121]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]

我不知道如何解决这个问题。这是我在js中的代码:

var sock = new SockJS('http://localhost:8080/core/privateChat');
var stomp = Stomp.over(sock);

stomp.connect('guest', 'guest', function(frame) {
    stomp.subscribe("/user/queue/notifications", handleNotification);
});

function handleNotification(message) {
    console.log('Message: ', message);
}

function sendMessage(text, re) {
    stomp.send("/app/privateChat", {}, JSON.stringify({
        'message' : text,
        'to' : re
    }));
}

我在java config中的配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/privateChat").setAllowedOrigins("*").withSockJS();
    }

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

和处理这个websocket的功能:

@Autowired
private SimpMessagingTemplate messaging;

@MessageMapping("/privateChat")
@SendToUser("/queue/notifications")
public void handleprivateChat(Message message, Principal principal) {
    messaging.convertAndSend("/queue/notifications", new Message(StatusMessage.INFO, "test"));
}

提前感谢您的帮助。

0 个答案:

没有答案