使用Websockets时,无法在Spring Controller中获得ServletContext

问题描述:

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("/agent-sendstatus") 
     public void testmethod() 
     { 
      //How do i get ServletContext here to further implement solution? 
      template.convertAndSend("/topic/agent-sendstatus","bcd"); 
     } 

请建议。 我在控制器中使用SimpMessageHeaderAccessor获取会话,但我也需要ServletContext。 春天有什么方法吗?使用Websockets时,无法在Spring Controller中获得ServletContext

+0

你真的从背景中期待什么? – Rafa 2014-09-25 06:06:02

+0

那么,我需要从上下文的所有属性。 – abcd 2014-09-25 08:31:58

+0

你的回答对我来说已经很清楚了,但你到底需要什么? sergi的答案是真实的。在websockets中,只有在开始时(在握手过程中)才有HTTP,所有进一步的通信都通过一个套接字。也许还有另外一种方法来获取你需要的信息(考虑到你知道你需要的信息) – Rafa 2014-09-25 11:28:11

当您升级到WebSocket时,通信中不涉及HTTP,唯一握手的地方是HTTP。您可以实现HandshakeInterceptor以获取ServletContext并将任何参数公开给WebSocket会话,因此您可以使用HeaderAccessor将它们放入消息处理程序方法中。查看HttpSessionHandshakeInterceptor的示例。