websocket connection

bedda13 days 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 Tananaev13 days 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

bedda13 days ago

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

Anton Tananaev13 days ago

Your browser passes it automatically.

Anton Tananaev13 days 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.

bedda13 days 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);
        };
bedda13 days ago

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

bedda13 days ago

it still doesnt work with the new code

Anton Tananaev13 days ago

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

bedda13 days 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 Tananaev13 days ago
bedda13 days 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 Tananaev13 days ago

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