subscribing to events

This commit is contained in:
2021-05-22 22:36:13 +02:00
parent 1f49ef9f67
commit 998e93968f
2 changed files with 68 additions and 15 deletions

View File

@ -23,38 +23,86 @@ function createChatWindow() {
let win = document.createElement("div");
win.id = "chat-win";
console.log("Creating chat window");
win.innerHTML = `
<div id="message-win">
<p>Test Message</p>
</div>
<input id="sendMessage" name="message" placeholder="Very important message..." type="text">
<form id="message-form">
<input id="message-input" name="message" placeholder="Very important message..." type="text">
</form>
`;
win.getElementById("sendMessage").addEventListener("keyup", (evt) => {
if (evt.keyCode === 13) {
evt.preventDefault();
//TODO: Send Message
}
let messageForm = win.getElementsByTagName("form")[0];
messageForm.addEventListener("submit", (evt) => {
evt.preventDefault();
let messageInput = messageForm.elements['message-input'];
let message = JSON.stringify({
room_id: "60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628",
data: {
message: messageInput.value
}
})
sendEvent(message).then((evt => {
console.log(evt)
}));
let messageAdd = document.createElement("p");
messageAdd.innerHTML = `You: <strong> ${messageInput.value} </strong>`;
win.getElementsByTagName("div")[0].appendChild(messageAdd);
messageInput.value = "";
});
while (windows.lastChild) windows.removeChild(windows.lastChild);
windows.style.visibility = "hidden";
subscribeEvent();
main.appendChild(win);
}
async function sendEvent(message) {
let res = await fetch(dest, {
return fetch(`${dest}/_usimp/send_event`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `usimp ${token}`,
'From-Domain': domain,
'To-Domain': domain,
},
body: message
});
let result = await res;
alert(result.message);
}
async function subscribeEvent() {
let response = await fetch(`${dest}/_usimp/subscribe`, {
method: 'POST',
headers: {
'To-Domain': domain,
'From-Domain': domain
},
body: JSON.stringify({})
});
if (response.status !== 200) {
showMessage(response.statusText);
await new Promise(resolve => setTimeout(resolve, 1000));
await subscribeEvent();
} else {
let message = await response.text();
console.log(message);
addMessage(JSON.parse(message).event.data.message);
await subscribeEvent();
}
}
function addMessage(message) {
let messageContainer = document.getElementById("message-win");
let messageP = document.createElement("p");
messageP.innerHTML = `Not You: <strong> ${message} </strong>`;
messageContainer.appendChild(messageP);
}
function createLoginWindow() {
let win = document.createElement("div");
@ -93,15 +141,20 @@ function createLoginWindow() {
console.log(data["domain"]);
let domainServer = usimp.chooseDomainServer(data["domain_servers"]);
console.log(domainServer);
dest = "http://" + domainServer.host + ':' + domainServer.protocols.http + "/"
dest = `http://${domainServer.host}:${domainServer.protocols.http}`;
domain = form.elements["domain"].value
usimp.login(domainServer, data["domain"].id, form.elements["account"].value, form.elements["password"].value)
.then(res => res.json())
.catch((evt) => {
console.log("Serva ded");
console.log(evt);
})
.then(data => {
token = data.token;
console.log(data.token);
})
createChatWindow();
});
})
.catch(reason => {
console.error(reason);

View File

@ -33,7 +33,7 @@ usimp.chooseDomainServer = function (domainServers, invalidDomainServers = []) {
}
usimp.login = function (domainServer, domain, account, password) {
return fetch('http://' + domainServer.host + ':' + domainServer.protocols.http + '/_usimp/authenticate', {
return fetch(`http://${domainServer.host}:${domainServer.protocols.http}/_usimp/authenticate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',