39 lines
978 B
JavaScript
39 lines
978 B
JavaScript
function getCookie(name) {
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop().split(';').shift();
|
|
}
|
|
|
|
$(document).ready(() => {
|
|
//prod
|
|
// const socket = new WebSocket('wss://auth.foxarmy.org');
|
|
//dev
|
|
const socket = new WebSocket('ws://localhost:3000');
|
|
|
|
socket.onmessage = message => {
|
|
console.log(message)
|
|
}
|
|
|
|
const sendData = async () => {
|
|
const jwt = getCookie("jwt");
|
|
const author = await (await fetch(`/api/getUsername`)).json()
|
|
const content = $("#chat-input").val();
|
|
|
|
const message = {
|
|
jwt, author, content
|
|
};
|
|
|
|
socket.send(JSON.stringify(message));
|
|
}
|
|
|
|
$("#send-message-button").click(sendData);
|
|
|
|
$('#chat-input').keyup(function(e){
|
|
if(e.keyCode == 13)
|
|
{
|
|
$(this).trigger("enterKey");
|
|
}
|
|
});
|
|
|
|
$("#chat-input").bind("enterKey", sendData);
|
|
}); |