From 2c022fd8bb94a7670161bcd186a36c08e5eb0d75 Mon Sep 17 00:00:00 2001 From: Xander Date: Wed, 4 Oct 2023 16:30:22 +0200 Subject: [PATCH 1/2] send a message to kelder using socket test --- content/assets/scripts/message.js | 31 ++++++++++++++----------------- content/cammie.erb | 4 ---- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/content/assets/scripts/message.js b/content/assets/scripts/message.js index 280bc25d..007bc133 100644 --- a/content/assets/scripts/message.js +++ b/content/assets/scripts/message.js @@ -5,28 +5,25 @@ const formButton = form.find('button[type="submit"]'); const responseError = $("#cammieFormResponseError"); const responseSuccess = $("#cammieFormResponseSuccess"); +const socket = io("https://kelder.zeus.ugent.be"); + +socket.on('replymessage', function(obj) { + alert("Kelder says: " + obj['message']); +}) + $("#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() { + socket.timeout(4000).emit("message",{username: formName.val(), message: formMessage.val()},(err,_) => { + if (err) { + responseSuccess.addClass("is-hidden"); + responseError.removeClass("is-hidden"); + responseError.text("Something went wrong, please try again later."); + } else { responseError.addClass("is-hidden"); responseSuccess.removeClass("is-hidden"); responseSuccess.text("Message has been successfully sent."); formMessage.val(""); - }, - error() { - responseSuccess.addClass("is-hidden"); - responseError.removeClass("is-hidden"); - responseError.text("Something went wrong, please try again later."); - }, - complete() { - formButton.removeClass("is-loading"); - }, - }); + } + }) }); diff --git a/content/cammie.erb b/content/cammie.erb index 20ce22f1..417b6544 100644 --- a/content/cammie.erb +++ b/content/cammie.erb @@ -6,10 +6,6 @@ title: Cammie <%= asset :js, :cammie %> <%= asset :js, :message %> From fa277c126ff32087d16295f3360c2ab6f50cf6d9 Mon Sep 17 00:00:00 2001 From: Xander Date: Thu, 7 Dec 2023 22:20:07 +0100 Subject: [PATCH 2/2] Working sockets --- content/assets/scripts/message.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/assets/scripts/message.js b/content/assets/scripts/message.js index 007bc133..39c543bf 100644 --- a/content/assets/scripts/message.js +++ b/content/assets/scripts/message.js @@ -14,7 +14,7 @@ socket.on('replymessage', function(obj) { $("#cammieForm").submit((e) => { e.preventDefault(); formButton.addClass("is-loading"); - socket.timeout(4000).emit("message",{username: formName.val(), message: formMessage.val()},(err,_) => { + socket.timeout(3000).emit("message",{username: formName.val(), message: formMessage.val()},(err,_) => { if (err) { responseSuccess.addClass("is-hidden"); responseError.removeClass("is-hidden"); @@ -25,5 +25,6 @@ $("#cammieForm").submit((e) => { responseSuccess.text("Message has been successfully sent."); formMessage.val(""); } + formButton.removeClass("is-loading"); }) });