Skip to content

Commit

Permalink
send a message to kelder using socket test
Browse files Browse the repository at this point in the history
  • Loading branch information
xerbalind committed Dec 7, 2023
1 parent 87bdc8b commit 2c022fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
31 changes: 14 additions & 17 deletions content/assets/scripts/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
});
}
})
});
4 changes: 0 additions & 4 deletions content/cammie.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ title: Cammie
<script src="https://code.jquery.com/jquery-3.0.0.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.js" integrity="sha256-ohm5bQuo4yRByZrO3JFkHnUxob0EsucKvzTA4ZUQ87g=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
var socket = io("https://kelder.zeus.ugent.be");
socket.on('replymessage', function(obj) {
alert("Kelder says: " + obj['message']);
})
</script>
<%= asset :js, :cammie %>
<%= asset :js, :message %>
Expand Down

0 comments on commit 2c022fd

Please sign in to comment.