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 Oct 5, 2023
1 parent 140be07 commit 8387a39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
57 changes: 40 additions & 17 deletions content/assets/scripts/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,51 @@ 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.emit("message",{username: formName.val(), message: formMessage.val()})
/* 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() { */
/* 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 8387a39

Please sign in to comment.