From 11e6da0a9b662bb5ffd17fa60e19c9cfd1dc9b3a 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 | 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 b75898a5..4610ab99 100644 --- a/content/cammie.erb +++ b/content/cammie.erb @@ -6,10 +6,6 @@ title: Cammie <%= asset :js, :cammie %> <%= asset :js, :message %>