Can't establise socket connection on api/socket

Vinit Singh3 years ago

I want to get live locations through socket connection. I am not able to receive it

I tried this example: https://github.com/traccar/traccar-web/blob/master/web/simple/app.js

I set my url and token into this but it is not working for me.

So i tried jquery ajax for session api

    $.ajax({
        url: "{host}/api/session",
        dataType: "json",
        type: "POST",
        data: {
            email: "username",
            password: "password"
        },
        success: function(sessionResponse){
            console.log(sessionResponse);
        }
    });

this is giving me set-cookie header in response..

So after successfull session api i tried this for socket connection but it is always failing

   var socket = new WebSocket('ws' + url.substring(4) + '/api/socket');

    socket.onclose = function (event) {
        console.log('socket closed');
    };

    socket.onmessage = function (event) {
        var data = JSON.parse(event.data);
        console.log(data);
    };

How can i establish socket connection after successfull session api request.?

Anton Tananaev3 years ago

What's the URL? Are you connecting to a different host?

Vinit Singh3 years ago

I am trying to connect to url: {host-ip}:8080 from.. my website domain({domain}.com).

Anton Tananaev3 years ago

Have you configured CORS correctly? Please provide details.