Wolfone
Wolfone

Reputation: 1436

Websocket over wss in context of Spring + Angular + Tyrus

Following situation:

I have Spring-Boot-backend which communicates with an angular frontend. They are both packaged together into an executable war.

Until recently it used http and ws. Now I want to migrate to using https and wss.

The transition to https worked just fine and the flow of events goes well for the combination of https and ws.

This is my (shortened) function on the angular side triggering the connection upgrade:

upgradeConnectionToWS() {
    const app = this;
    return new Promise<WebSocket>(function (resolve, reject) {

        let socket: WebSocket = new WebSocket("ws://localhost:8025/ws/notify");

        socket.onopen = function (event) {
            [...]
            resolve(socket);
        };

        [...onmessage, onerror,...]
    });
}

My ServerEndpoint (shortened):

    @javax.websocket.server.ServerEndpoint(
        value = "/notify",
        encoders = JsonMessageEncoder.class,
        decoders = JsonMessageDecoder.class,
        configurator = WSEndpointConfigurator.class
    )
    public class WsServerEndpoint{
   
        @OnOpen
        public void onOpen(Session session, EndpointConfig config) {
            System.out.println(format("%s initiated ws-connection.", session.getId()));
            [...logging stuff, pushing variables...]
        }
    }

The WsEndpointConfigurer mainly logs Headers.

My Main looks like this:

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        org.glassfish.tyrus.server.Server server = 
            new org.glassfish.tyrus.server.Server("localhost", 8025, "/ws", WsServerEndpoint.class);
        try {
            server.start();
        } catch (DeploymentException e) {
            throw new RuntimeException(e);
        }
    }

Relevant dependencies:

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-server</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly</artifactId>
        <version>1.1</version>
    </dependency>

Now when I switch to wss in the angular part I only get timeouts on the upgrade-requests.

So far this: https://stackoverflow.com/a/53867046 was one of the few things I found on the subject but it wasn't helpful to me to be honest.

I would be thankful for any help. If there is more information needed, I will try to provide it.

Upvotes: 1

Views: 193

Answers (0)

Related Questions