2014-09-15 6 views
2

스프링 MVC 프레임 워크와 함께 웹 소켓 SockJS를 사용하고 있습니다. 주식 시세 표시기 예제가 잘 작동했지만 지금은 내 컨트롤러에서 세션을 가져오고 싶지만 출구를 찾을 수 없습니다. 경우 사용자가 웹 소켓 세션 언급하는웹 소켓을 사용할 때 스프링 컨트롤러에서 세션을 가져올 수 없습니다.

Client Code: 
$scope.socket = new SockJS("ws/ws"); 
$scope.stompClient = Stomp.over($scope.socket); 
$scope.stompClient.connect("guest", "guest",connectCallback, errorCallback); 

//in connectCallback 
$scope.stompClient.subscribe('/topic/agent-sendstatus', showScreenPop); 

    Java Code: 
    @MessageMapping("/topic/agent-sendstatus") 
     public void testmethod() 
     { 
      //How do i get Session here to further implement solution? 

      template.convertAndSend("/topic/agent-sendstatus","bcd"); 
     } 

Please suggest. 

답변

6

, 스프링 4.1는 세션이 SimpMessageHeaderAccessor를 통해 액세스 할 수있는 수신 클라이언트 메시지의 헤더에 속성을 얻을 수 있습니다.

@MessageMapping("/topic/agent-sendstatus") 
public void testmethod(SimpMessageHeaderAccessor headerAccessor) { 
    String sessionId = headerAccessor.getSessionId(); // Session ID 
    Map<String, Object> attrs = headerAccessor.getSessionAttributes(); // Session Attributes 
    ... 
} 
+0

이 저를 도와 그래서 나는 Spring을 업데이트하지 않고 당신의 솔루션을 시도했다. – abcd

3

또한 인터셉터가 필요하다고 생각하면 비어있는 sessionAttributes이 표시됩니다.

당신은 dispatcher-servlet.xmlHttpSessionHandshakeInterceptor을 추가해야합니다

<websocket:message-broker 
    application-destination-prefix="/app" 
    user-destination-prefix="/user"> 
    <websocket:stomp-endpoint path="/websocket"> 
     <websocket:handshake-interceptors> 
      <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/> 
     </websocket:handshake-interceptors> 
     <websocket:sockjs session-cookie-needed="true" /> 
    </websocket:stomp-endpoint> 
    <websocket:simple-broker prefix="/topic, /message" /> 
</websocket:message-broker> 

을 그리고 당신은 컨트롤러에서 세션을 얻을 수있을 것입니다 : 나는 봄 4.0.6를 사용하고 있지만

@MessageMapping("/authorization.action") 
public Message authorizationAction(
     SimpMessageHeaderAccessor headerAccessor, Message message) { 
    Map<String, Object> sessionAttributes = headerAccessor.getSessionAttributes(); 
    System.out.println(sessionAttributes); 
    // Do something with session 
    return new Message(...); 
} 
관련 문제