From 29af9ffb5db1ec4174775203f5717eb40b1e4228 Mon Sep 17 00:00:00 2001 From: Xander Date: Wed, 4 Oct 2023 16:30:22 +0200 Subject: [PATCH] send a message to kelder using socket test --- content/assets/scripts/message.js | 43 +++++++++++++++++++------------ content/cammie.erb | 4 --- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/content/assets/scripts/message.js b/content/assets/scripts/message.js index 280bc25d..15a9096c 100644 --- a/content/assets/scripts/message.js +++ b/content/assets/scripts/message.js @@ -5,28 +5,37 @@ const formButton = form.find('button[type="submit"]'); const responseError = $("#cammieFormResponseError"); const responseSuccess = $("#cammieFormResponseSuccess"); -$("#cammieForm").submit((e) => { - e.preventDefault(); - formButton.addClass("is-loading"); - return $.ajax({ - url: "https://kelder.zeus.ugent.be/messages/", - contentType: "text/plain", - type: "POST", - headers: formName.val() ? { "X-Username": formName.val()} : {}, - data: formMessage.val(), - success() { +const socket = io("https://kelder.zeus.ugent.be"); + +socket.on('replymessage', function(obj) { + alert("Kelder says: " + obj['message']); +}) + +socket.on('status',function(data){ + console.log("reply") + if (data.ok){ responseError.addClass("is-hidden"); responseSuccess.removeClass("is-hidden"); responseSuccess.text("Message has been successfully sent."); formMessage.val(""); - }, - error() { + } else { responseSuccess.addClass("is-hidden"); responseError.removeClass("is-hidden"); responseError.text("Something went wrong, please try again later."); - }, - complete() { - formButton.removeClass("is-loading"); - }, - }); + } + formButton.removeClass("is-loading"); + /* return data.message; */ +}) + +$("#cammieForm").submit((e) => { + e.preventDefault(); + formButton.addClass("is-loading"); + socket.timeout(4000).emit("message",{username: formName.val(), message: formMessage.val()},(err,response) => { + if (err) { + // the other side did not acknowledge the event in the given delay + console.log(err); // "got it" + } else { + console.log(response); // "got it" + } + }) }); diff --git a/content/cammie.erb b/content/cammie.erb index b75898a5..4610ab99 100644 --- a/content/cammie.erb +++ b/content/cammie.erb @@ -6,10 +6,6 @@ title: Cammie <%= asset :js, :cammie %> <%= asset :js, :message %>