subscribing to events
This commit is contained in:
@ -23,38 +23,86 @@ function createChatWindow() {
|
|||||||
let win = document.createElement("div");
|
let win = document.createElement("div");
|
||||||
win.id = "chat-win";
|
win.id = "chat-win";
|
||||||
|
|
||||||
|
console.log("Creating chat window");
|
||||||
|
|
||||||
win.innerHTML = `
|
win.innerHTML = `
|
||||||
<div id="message-win">
|
<div id="message-win">
|
||||||
<p>Test Message</p>
|
<p>Test Message</p>
|
||||||
</div>
|
</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) {
|
let messageForm = win.getElementsByTagName("form")[0];
|
||||||
evt.preventDefault();
|
messageForm.addEventListener("submit", (evt) => {
|
||||||
//TODO: Send Message
|
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);
|
main.appendChild(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendEvent(message) {
|
async function sendEvent(message) {
|
||||||
let res = await fetch(dest, {
|
return fetch(`${dest}/_usimp/send_event`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `usimp ${token}`,
|
'Authorization': `usimp ${token}`,
|
||||||
|
'From-Domain': domain,
|
||||||
|
'To-Domain': domain,
|
||||||
},
|
},
|
||||||
body: message
|
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() {
|
function createLoginWindow() {
|
||||||
let win = document.createElement("div");
|
let win = document.createElement("div");
|
||||||
@ -93,15 +141,20 @@ function createLoginWindow() {
|
|||||||
console.log(data["domain"]);
|
console.log(data["domain"]);
|
||||||
let domainServer = usimp.chooseDomainServer(data["domain_servers"]);
|
let domainServer = usimp.chooseDomainServer(data["domain_servers"]);
|
||||||
console.log(domainServer);
|
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)
|
usimp.login(domainServer, data["domain"].id, form.elements["account"].value, form.elements["password"].value)
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
|
.catch((evt) => {
|
||||||
|
console.log("Serva ded");
|
||||||
|
console.log(evt);
|
||||||
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
token = data.token;
|
token = data.token;
|
||||||
console.log(data.token);
|
console.log(data.token);
|
||||||
})
|
createChatWindow();
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(reason => {
|
.catch(reason => {
|
||||||
console.error(reason);
|
console.error(reason);
|
||||||
|
@ -33,7 +33,7 @@ usimp.chooseDomainServer = function (domainServers, invalidDomainServers = []) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
usimp.login = function (domainServer, domain, account, password) {
|
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',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
Reference in New Issue
Block a user