var socketLink = null


fetch('/internal/socketlink', {
    method: "GET"
}).then(link => {socketLink = link})

/**
 * @type {SocketIOClient.Socket}
 */
const socket = io(socketLink);



function get(request) {

    return new Promise((resolve, reject) => {

        socket.emit("GET/" + request)
        console.log("Envoi de la requête GET : " + request)
        
        socket.once("ANSWER/GET/" + request, (answer) => {
            
            console.log("Réponse pour la requête GET : " + request)
       
            resolve(answer)
                    
        })
        
    })
    
}

function post(request, data) {

    return new Promise((resolve, reject) => {

        socket.emit("POST/" + request, data)
        console.log("Envoi de la requête POST : " + request)
        
        socket.once("ANSWER/POST/" + request, (answer) => {
            
            console.log("Réponse pour la requête POST : " + request)
            
            resolve(answer)
                    
        })
        
    })
    
}

function AlwaysRequest(GQname, GQcallback) {
    socket.on("ALWAYS/" + GQname, (data) => {
    
        console.log("Récéption pour la requête ALWAYS : " + GQname)
        GQcallback(data)
    })

}

function getSocket() {

    return socket
}
