WebFM/src/core/static/js/webfm/sse.js

33 lines
921 B
JavaScript
Raw Normal View History

2023-02-25 21:37:15 +00:00
const sse_id = getCookie("sse_id");
const publishURL = `https://www.webfm.com/sse/${sse_id}/sse`;
const subscribeURL = publishURL;
const eventSource = new EventSource(subscribeURL);
// ---- Incoming events ---- //
2023-02-25 21:37:15 +00:00
// eventSource.onopen = (eve) => {
// console.log(eve);
// };
2023-02-25 21:37:15 +00:00
eventSource.onerror = (eve) => {
console.log(publishURL);
console.log(eve);
};
2023-02-25 21:37:15 +00:00
eventSource.onmessage = (eve) => {
try {
const data = JSON.parse(eve.data);
const sse_msg = JSON.parse(data.message);
if (sse_msg.hasOwnProperty('path') || sse_msg.hasOwnProperty('stream')) {
const target = (sse_msg.path) ? sse_msg.path : sse_msg.stream;
handleMedia(target);
return;
} else if (sse_msg.hasOwnProperty('message')) {
displayMessage(sse_msg.message.text, sse_msg.message.type);
}
} catch (e) {
console.log(e);
}
};