-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-melding.js
33 lines (32 loc) · 1003 Bytes
/
send-melding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function send_message() {
const profile = localStorage.getItem('loggedinBruker');
const message = document.getElementById("message").value;
const chatroom = document.getElementById('chatroomName').textContent;
const timestamp = new Date().toLocaleString();
console.log(chatroom);
fetch(`http://192.168.1.243:3000/chatRoom`, {
method: "POST",
headers: {
"Content-type": "application/json"
},
body: JSON.stringify({
profile: profile,
message: message,
timestamp: timestamp,
chatroom: chatroom
})
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
console.log("Message sent successfully");
return response.json();
})
.then(data => {
// Handle the response data here if needed
})
.catch(error => {
console.error('Error sending message:', error);
});
}