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 140be07 commit 29af9ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
43 changes: 26 additions & 17 deletions content/assets/scripts/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@ 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.timeout(4000).emit("message",{username: formName.val(), message: formMessage.val()},(err,response) => {
if (err) {
// the other side did not acknowledge the event in the given delay
console.log(err); // "got it"
} else {
console.log(response); // "got it"
}
})
});
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 29af9ff

Please sign in to comment.