neutral/public/javascripts/io.js
Raphix 5e97ff4853
All checks were successful
Neutral/pipeline/head This commit looks good
Version 0.2.0 - PREVERSION - Ajout READ de l'explorateur
2023-11-03 14:25:39 +01:00

64 lines
1.2 KiB
JavaScript

// Socket IO - Communication
var socketLink = null
fetch('/internal/socketlink', {
method: "GET"
}).then(link => {socketLink = link})
const socket = io(socketLink);
socket.on("connect", () => {
console.log("Connecté au serveur par le Socket avec l'ID : " + socket.id)
});
socket.on("disconnect", () => {
window.location.href = "/"
})
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 : " + request)
resolve(answer)
})
})
}
function post(request, value) {
return new Promise((resolve, reject) => {
socket.emit("POST/" + request, value)
console.log("Envoi de la requête POST : " + request)
socket.once("ANSWER/POST/" + request, (answer) => {
console.log("Réponse pour la requête : " + request)
resolve(answer)
})
})
}
function getSocket() {
return socket
}