websocket connection

bedda 2 years ago

hi there , im trying to use to connect the websocket to the server but no luck,i did get the session cookie:JSESSIONID=node01adwd8tgcec1x4r7bkfh7sl64584.node0 and then pass it in the websocket:

 const establishWebSocketConnection = (cookie) => {
        const socket = new WebSocket("wss://demo.traccar.org/api/socket", [], {
            'Cookie': cookie
        });
        socket.onopen = () => {
            console.log('WebSocket connection established');
        };

        socket.onerror = (error) => {
            console.log('WebSocket connection failed:', error);
            console.log('Ready state:', socket.readyState);
            console.log('Error event:', error.event);
        };
        setSocket(socket);
    };

can someone help me

Anton Tananaev 2 years ago

Where did you get this from? WebSocket constructor only has two arguments, so your code makes absolutely no sense.

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket

bedda 2 years ago

can u tell me how to pass the cookie then?
thank you

Anton Tananaev 2 years ago

Your browser passes it automatically.

Anton Tananaev 2 years ago

Still curious where you found that code. I'm sure you didn't just write random nonsense. There must be some resource where you found it.

bedda 2 years ago

so it should work like this:

 const establishHttpSession = async () => {
        await axios.get('https://demo.traccar.org/api/session?token=<my-token>);

        const socket = new WebSocket("wss://demo.traccar.org/api/socket");


        socket.onopen = () => {
            console.log('WebSocket connection established');
        };

        socket.onerror = (error) => {
            console.log('WebSocket connection failed:', error);
            console.log('Ready state:', socket.readyState);
            console.log('Error event:', error.event);
        };
bedda 2 years ago

i found it on stackoverflow , i thought it was correct,mb ,i will try find the post

bedda 2 years ago

it still doesnt work with the new code

Anton Tananaev 2 years ago

I hope you're not actually using "demo.traccar.org". Are you familiar with CORS restrictions?

bedda 2 years ago

i cant use the demo to connect to the websocket?
and no i'm not familiar with CORS restrictions, im still beginner ,im using traccar for a college project

Anton Tananaev 2 years ago
bedda 2 years ago

hey anton,so the CORS is not enable in the demo ,so i tried to set up a proxy:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
    app.use(
        '/api/socket',
        createProxyMiddleware({
            target: 'https://demo4.traccar.org',
            changeOrigin: true,
            ws: true,
        })
    );
};

and this the connection code:

  const establishHttpSession = async () => {

            await axios.get(
                'https://demo4.traccar.org/api/session?token=<mytoken>
            );
    };
useEffect(() => {
        const establishConnection = async () => {
            await establishHttpSession();

            const protocol =  'wss:'
            const host = window.location.host

            const wsUrl =`${protocol}//${host}/api/socket`;
            const socket = new WebSocket(wsUrl);

            socket.onopen = () => {
                console.log('WebSocket connection established');
            };
            socket.onerror = (error) => {
                console.error('WebSocket error:', error);
            };
            socket.onmessage = (message) => {
                console.log('WebSocket message:', message);
            };
            socket.onclose = (event) => {
                console.log('WebSocket connection closed:', event);
            };
        }

        establishConnection();
    }, [])

the console just return WebSocket connection closed

Anton Tananaev 2 years ago

I think you still don't understand CORS. You're creating a session with different host.