diff --git a/content/assets/scripts/message.js b/content/assets/scripts/message.js index 280bc25d..39c543bf 100644 --- a/content/assets/scripts/message.js +++ b/content/assets/scripts/message.js @@ -5,28 +5,26 @@ 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(3000).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"); - }, - }); + } + 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 %>