向不在STOMP,sockjs,spring

时间:2018-01-19 20:11:56

标签: spring sockets websocket stomp sockjs

我遇到Spring和Stomp的问题,客户端没有收到这些消息。我已经尝试了一切,但不确定是什么导致它!

到目前为止,我有以下设置。第一个subscribe / event / {eventId}正常工作并正在接收消息。特定用户订阅到达控制器,但未收到来自convertandsendtouser的响应。有什么想法吗?

function connect() {
    var socket = new SockJS('/events');
    stompClient = Stomp.over(socket);  
    stompClient.connect({}, function(frame) {

        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/' + eventIdStomp, function(messageOutput) {
            showMessageOutput(JSON.parse(messageOutput.body));
            alert("Subscribed to event id");
        });

        stompClient.subscribe("/user/queue/reply", function(responseOutput) {
            alert("Hey it worked, subscribed to user queue");
            showMessageOutput(JSON.parse(responseOutput.body));
        });
    });
}

function sendMessage() {
    var text = document.getElementById('text').value;
    stompClient.send("/app/events/" + eventIdStomp, {}, 
      JSON.stringify({'from':from, 'text':text}));  
    stompClient.send("/app/events/personal", {}, 
            JSON.stringify({'from':from, 'text':text}));  
}

并在服务器端

@MessageMapping("/events/personal")
    public void personal(Message message, Principal principal) throws Exception {

        System.out.println("im in side personal methods");
        String time = new SimpleDateFormat("HH:mm").format(new Date());

        /* Set author */
        User user = (User) ((Authentication) principal).getPrincipal();
        if(user!=null) { 

            System.out.println("in inside user id");
            /* Check message content in Knowledge Base */
            // If there is any indication that the message contains material against the code of conduct,
            // then a message should be sent to that person only and not to everybody.
            OutputMessage custom_response = new OutputMessage(user.getUsername(), "I can see you...", time);
            simpMessagingTemp.convertAndSendToUser(user.getUsername(), "/queue/reply", custom_response);    
            // End of KB
            System.out.println("after mnessage sent");
        }
    }
使用配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        /* RabbitMQ As Broker, using CloudAMQP as a cloud service */
        config.enableStompBrokerRelay("/queue", "/topic").setRelayHost("swan.rmq.cloudamqp.com")
                /* System admin login */
                .setSystemLogin("yyy").setSystemPasscode("xxx")
                .setVirtualHost("yyy")
                /* for presentation purposes, client can login as system admin */
                .setClientLogin("yyy").setClientPasscode("xxx");
        config.setApplicationDestinationPrefixes("/app");
    }

    /*
     * When we create a connection from client, this is the URL which clients
     * connect to Websocket URL prefix
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/events").withSockJS().setSessionCookieNeeded(true);
    }
}

0 个答案:

没有答案