2017-01-17 5 views
1

작동하지 구독 .. 여기에 내 코드입니다 : 서버내가 클라이언트 뭔가 서버에서 전송하는 웹 소켓을 사용하려면 봄 SockJs 테야

---- 웹 소켓 구성 ----

@Configuration 
@EnableWebSocketMessageBroker 
@Slf4j 
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{ 

@Override 
public void configureMessageBroker(MessageBrokerRegistry config) { 
    config.enableSimpleBroker("/topic"); 
} 

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

---- 컨트롤러 ---

@Controller 
@Component 
@Slf4j 
public class MenuItemController{ 
    @SendTo("/topic/notification") 
    public String sendToMenuItems(MenuItemDto menuItem) { 
     return menuItem.getMenuItemName(); 
    } 
} 

----- 클라이언트 ----

var SockJS = require('sockjs-client'); 
var Stomp = require('stompjs'); 

let connectWebSocket =() => { 
    socket = new SockJS(context.backend + '/myWebSocketEndPoint'); 
    stompClient = Stomp.over(socket); 
    stompClient.connect({},function (frame) { 
    console.log(frame); 
    stompClient.subscribe('/topic/notification', function(menuItem){ 
     alert(menuItem); 
    }); 
    }); 
} 
connectWebSocket(); 

webpack을 사용하기 때문에 context.backend를 사용하므로 websocket이 백엔드 서버에 연결됩니다. 그것은 연결을 위해 작동하고 구독도 작동하는 것처럼 보입니다. 그러나 서버에서 클라이언트로 무언가를 보낼 때 경고 메시지가 나타나지 않고 콘솔에 websocket에 들어오는 메시지가 없습니다. 내 문제가 뭐니? 나는 누군가가 나를 도울 수 있기를 바랍니다 .. = D와 관련된 문제가

답변

1

: https://github.com/spring-guides/gs-messaging-stomp-websocket/issues/10

사용

stompClient.connect({}, function(frame) { 

대신

stompClient.connect("","",function (frame) { 

편집 : 당신이 궁금해하는 경우 Rest Controller를 사용하여 SimpMessagingTemplate을 사용하여 메시지를 보내십시오.

@Autowired 
private SimpMessagingTemplate messagingTemplate; 

및 방법은없는 경우,

messagingTemplate.convertAndSend("/topic/notification", "test"); 

전화 @MessageMapping(value = "/sentToMenu")로 sendToMenuItems에 주석을해야하고 메시지를 보내는 적절한 스톰 클라이언트가됩니다 stompClient.send("/sentToMenu", {}, 'teste');

+0

감사를 많이하지만,이 내 문제를 해결하지 못합니다 .. – Catechacha

+0

브라우저 콘솔 (f12)을보고 전체 로그를 붙여 넣으십시오. 또한 발생할 수있는 cors 문제에 대한 레지스트리 개체에 .setAllowedOrigins ("*")를 넣는 것을 고려하십시오. –

+0

이 실제 예제를 참조하십시오 : https://github.com/cletogadelha/spring-socket-stomp –