user7716485
user7716485

Reputation:

Lost connection Spring Websocket after opening it

I have this WebSocket configuration:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

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

}

I send Kafka data through SimpMessagingTemplate like this:

public class KafkaReceiver {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @KafkaListener(topics = "metrics", groupId = "metrics")
    public void receive(ConsumerRecord<?, ?> record) {
        MetricRecord m = new MetricRecord(new Long(record.offset()), record.key().toString(), record.value().toString());
        this.simpMessagingTemplate.convertAndSend("/topic/records", m.toString());
    }
}

And I have this client:

function connect() {
    var socket = new SockJS('/portfolio/records');
    var stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/records', function (message) {
            console.log(message);
            console.log(JSON.parse(message.body));
        });
    });
}

When I run this in browser I get the following error:

Opening Web Socket...

GET http://localhost:9999/portfolio/records/info?t=1507027267841 404 (Not Found)
r._start @ sockjs.min.js:2
(anonymous) @ sockjs.min.js:2
setTimeout (async)
r @ sockjs.min.js:2
r @ sockjs.min.js:3
r @ sockjs.min.js:2
r._getReceiver @ sockjs.min.js:2
r.doXhr @ sockjs.min.js:2
(anonymous) @ sockjs.min.js:2
setTimeout (async)
r @ sockjs.min.js:2
r @ sockjs.min.js:2
connect @ metricsApp.js:2
onclick @ VM53 metrics:1

Whoops! Lost connection to http://localhost:9999/portfolio/records

Any thought on that? I could not found a working and different solution than mine. Thanks in advance.

Upvotes: 2

Views: 3831

Answers (2)

myratas
myratas

Reputation: 51

registry.addEndpoint("/portfolio").setAllowedOrigins("*").withSockJS();

did you try this on your StompEndpoint create?

Upvotes: 1

Edwin Diaz
Edwin Diaz

Reputation: 1763

Check this line:

var socket = new SockJS('/portfolio/records');

You should connect the socket to:

var socket = new SockJS('/${YourApplicationContextIfItExists}/${endPoint}')

assuming you don't have an application context, it should be

var socket = new SockJS('/portfolio')

Please note there could be other issues, but I think this might help.

Upvotes: 0

Related Questions