0

i am using this code to connect to my websocket spring boot, but it not working when im add header into.

import { Client, IMessage, IFrame } from '@stomp/stompjs'
import SockJS from 'sockjs-client'

interface StompConfig {
    onConnectCallback: (client: Client) => void
    onErrorCallback: (frame: IFrame) => void
    bearerToken: string
}

const connectStompClient = ({
    onConnectCallback,
    onErrorCallback,
    bearerToken,
}: StompConfig): Client => {
    const socket = new SockJS('http://localhost:8080/ws')
    const stompClient = new Client({
        webSocketFactory: () => socket as WebSocket,
        connectHeaders: {
            Authorization:
                'Bearer eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJraWVudXNlciIsInJvbGUiOiJVU0VSIiwiaWF0IjoxNzIwMjgwMTg4LCJleHAiOjE3MjAzNjY1ODh9.Z8TvOr0v_HepSOC3l2_niMM_b10niX_6vV97rxKAkXuIB1ig6r4mjh7oAJI6DqBE',
        },
        debug: (str) => {
            console.log(str)
        },
        reconnectDelay: 5000,
        onConnect: (frame: IFrame) => {
            console.log('Connected')
            onConnectCallback(stompClient)
        },
        onStompError: (frame: IFrame) => {
            console.error('Broker reported error: ' + frame.headers['message'])
            console.error('Additional details: ' + frame.body)
            onErrorCallback(frame)
        },
    })
    stompClient.activate()

    return stompClient
}

export default connectStompClient

How to add header in to my connection socket. if can not, What can i exchange to?

0

Browse other questions tagged or ask your own question.