2017-03-15 14 views
1

스프링 (버전 4.3.4.RELEASE)을 제공하는 SimpMessagingTemplate 및 simple-broker와 함께 websocket에 특히 문제가 있습니다. 나는 수업 시간에 사용자 메시지를 보내wildfly 8.2.1 + spring mvc + spring-websocket (단순 브로커) + sockjs + stomp

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    <async-supported>true</async-supported> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

내 컨트롤러 :

이 내 web.xml의 내용

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

입니다 :

@Autowired 
SimpMessagingTemplate simpMessagingTemplate; 

public void onMessage(final TextMessage message, Session session) throws JMSException { 
    .... 
    this.simpMessagingTemplate.convertAndSendToUser(
         userInfo.getIp(), "/queue/resp", message.getText()); 
} 

이 내 웹 소켓의 설정입니다

@Controller 공용 클래스 UserController는 TextWebSocketHand를 확장합니다. LER {

@MessageMapping("/req") 
@SendToUser(destinations = "/queue/resp") 
public String greeting(String greeting) 
     throws Exception { 
    System.out.println("greeeeeeeeeeeeeeeeeeeeeeeeeeeeeting: " + greeting); 
    return "{ \"date\": "+ new Date().getTime() + ", \"message\": " + greeting + "}"; 
} 

@MessageExceptionHandler 
@SendToUser("/queue/errors") 
public String handleException(Exception ex) { 
    ex.printStackTrace(); 
    System.out.println(ex); 
    return ex.getMessage(); 
} 

}

그리고 클라이언트에 내 JS 코드 :

<script> 
var service = {}, socket = { 
client: null, 
stomp: null 
}; 

service.userIpAddress = 'someIpAddress'; 
service.RECONNECT_TIMEOUT = 25000; 
service.SOCKET_URL = 'http://localhost:8080/examinee_workplace/rest/workplace/'; 


service.send = function(message) { 
console.info('try send message = ', message); 
var id = Math.floor(Math.random() * 1000000); 
socket.stomp.send("/app/req", { 
priority: 9 
}, JSON.stringify({ 
message: message, 
id: id 
})); 
}; 

var startListener = function() { 
console.info('subscribe'); 
socket.stomp.subscribe('/user/' + service.userIpAddress + '/queue/resp', function(resp) { 
console.warn('resp = ', resp); 
}); 
socket.stomp.subscribe('/user/' + service.userIpAddress + '/queue/errors', function(resp) { 
var body = JSON.parse(resp.body); 
console.warn('error body = ', body); 
}); 
}; 

var reconnect = function() { 
console.info('reconnect. this.RECONNECT_TIMEOUT = ', this.RECONNECT_TIMEOUT); 
setTimeout(service.initialize,1000); 
}; 

service.initialize = function() { 
socket.client = new SockJS(service.SOCKET_URL); 
socket.stomp = Stomp.over(socket.client); 
socket.stomp.connect({}, startListener); 
socket.stomp.onclose = reconnect; 
}; 

service.initialize(); 

그리고 마지막으로 내 문제는 - 내가 아닌 모든 클라이언트에 메시지가 나타납니다. 일부 메시지는 클라이언트에 전달되지 않습니다 (안티 바이러스 카스퍼 스키는 꺼져 있음). 이유가 있습니까?

답변