All files / js/websockets websocket-listener.ts

80% Statements 12/15
50% Branches 1/2
80% Functions 4/5
80% Lines 12/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                        15x   15x     15x     15x     440x         15x 15x 60x   15x     15x         15x   15x    
import BufferedStompClient from "./BufferedStompClient";
 
type Subscription = {
    route: string,
    callback: (message: WebsocketMessage) => void
}
 
export type WebsocketMessage = {
    body: string
}
 
// https://stomp-js.github.io/guide/stompjs/using-stompjs-v5.html
export const registerStomp = (subscriptions: Subscription[]) => {
    let websocketProtocol;
    Iif(location.protocol === 'https:') {
        websocketProtocol = "wss";
    } else {
        websocketProtocol = "ws";
    }
 
    const client = new BufferedStompClient({
        brokerURL: websocketProtocol + '://' + window.location.host + '/dynamic',
        debug: function (str: string) {
            console.log(str);
        },
        reconnectDelay: 300,
    });
 
    client.onConnect = _frame => {
        subscriptions.map(subscription =>
            client.subscribe(subscription.route, subscription.callback)
        );
        client.sendBuffer();
    };
 
    client.onStompError = frame => {
        console.log('Broker reported error: ' + frame.headers['message']);
        console.log('Additional details: ' + frame.body);
    };
 
    client.activate();
 
    return client;
}