mirror of
https://github.com/tanrax/simple-HTML-over-the-Wire-social-network.git
synced 2026-08-14 17:53:15 +02:00
41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
import { sendData } from '../webSocketsCli.js';
|
|
|
|
function addMessage() {
|
|
/*
|
|
VARIABLES
|
|
*/
|
|
const inputAuthor = document.querySelector("#message-form__author");
|
|
const inputText = document.querySelector("#message-form__text");
|
|
const inputSubmit = document.querySelector("#message-form__submit");
|
|
|
|
/*
|
|
FUNCTIONS
|
|
*/
|
|
|
|
/**
|
|
* Send new message
|
|
* @param {Event} event
|
|
* @return {void}
|
|
*/
|
|
function sendNewMessage(event) {
|
|
event.preventDefault();
|
|
// Prepare the information we will send
|
|
const newData = {
|
|
"action": "add message",
|
|
"data": {
|
|
"author": inputAuthor.value,
|
|
"text": inputText.value
|
|
}
|
|
};
|
|
// Send the data to the server
|
|
sendData(newData, window.myWebSocket);
|
|
// Clear message form
|
|
inputText.value = "";
|
|
}
|
|
|
|
// Sends new message when you click on Submit
|
|
inputSubmit.addEventListener("click", sendNewMessage);
|
|
|
|
}
|
|
|
|
export default addMessage(); |