1

I'm trying to connect a Spring Boot Stomp Server with multiple sockjs clients offline but I get the warning

Websocket is closed before the connection is established

followed by

GET http://192.168.1.45:8080/socket/327/si5osugt/jsonp?c=jp.a3xdefl net::ERR_ABORTED 404 (Not Found)

I'm using Spring Boot Version 2.1.2 with the spring-boot-starter-websocket package on the backend side and on the frontend side I'm using Angular 6 with sockjs-client version 1.3.0. Frontend and backend are both running on port 8080

I'm getting some errors while turning the internet down. If the internet is turned off the iframe tries to reach to https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.js.

I managed by configuring stomp server on the backend to set the client library by adding .setClientLibraryUrl to a absolute path which is offline reachable.

registry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS).setClientLibraryUrl("http://192.168.1.45/dist/sockjs.min.js");

and get a 200 OK on http://192.168.1.45/dist/sockjs.min.js

Spring Boot:

WebSocketConfiguration (extends AbstractWebSocketMessageBrokerConfigurer)

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/socket")
            .setAllowedOrigins("*")
            .withSockJS().setClientLibraryUrl("http://192.168.1.45/dist/sockjs.min.js");
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    MessageBrokerRegistry messageBrokerRegistry = registry.setApplicationDestinationPrefixes("/app");
    messageBrokerRegistry.enableSimpleBroker( "/test", "/test2"
    );
}

WebSocketController

private final SimpMessagingTemplate template;

@Autowired
WebSocketController(SimpMessagingTemplate template){
    this.template=template;
}

@MessageMapping("/send/message")
public void onReceivedMessage( String destination , String message){
    this.template.convertAndSend(destination , message);
}

public void convertAndSend(String url, Object o){
    this.template.convertAndSend(url, o);
}

Angular 6:

TestComponet

ngAfterViewInit() {
  let ws = new SockJS('http://192.168.1.45:8080/socket');
  this.stompClient = Stomp.over(ws);
  let that = this;
  that.stompClient.subscribe("/test", (message) => {
    if (message.body) {
     console.log(message.body);
    }
  });
  that.stompClient.subscribe("/test2", (message) => {
    if (message.body) {
     console.log(message.body);
    }
  });
}

I thought it would work by just adding the sockjs client lib to an offline reachable path but I get the warning

Websocket is closed before the connection is established

followed by

"GET http://192.168.1.45:8080/socket/327/si5osugt/jsonp?c=jp.a3xdefl net::ERR_ABORTED 404 (Not Found)"

The library works with an internet connection perfectly fine, but I need it to work with both situations online and offline.

Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32

1 Answers1

1

I had the same issue, and I fixed it by removing SockJs. So now I'm currently using only Stomp-Websockets.

Changes in SpringBoot-Service(WebsocketConfiguration):

registry.addEndpoint("/justStomp").setAllowedOrigins("*");

I removed the .withSockJS() and .setClientLibraryUrl(../sockjs.min.js)

Changes in my Javascript-Code to connect to the websocket:

  const stompClient = Stomp.client(`ws://localhost:8080/justStomp`);
  stompClient.heartbeat.outgoing = 0;
  stompClient.heartbeat.incoming = 0;
  stompClient.connect({ name: 'test' }, frame => this.stompSuccessCallBack(frame, stompClient), err => this.stompFailureCallBack(err));

Instead of using Stomp.over(sockjs) I use the Stomp.client Method to directly connect to the websocket-url. I have a rabbitMQ in the background with stomp-plugin, and this only works properly with the 2 heartbeat-settings. see here RabbitMQ Web STOMP without SockJS