websocket connection

beddaa year 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 Tananaeva year 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

beddaa year ago

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

Anton Tananaeva year ago

Your browser passes it automatically.

Anton Tananaeva year 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.

beddaa year 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);
        };
beddaa year ago

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

beddaa year ago

it still doesnt work with the new code

Anton Tananaeva year ago

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

beddaa year 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 Tananaeva year ago
beddaa year 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 Tananaeva year ago

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