forked from mbk-lab/rui_orig
2
0
Fork 0
rui/app_socket.js

68 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-01-15 16:13:46 +03:00
let socket
let socketUrl
2022-10-27 16:14:30 +03:00
function sendMessage(message) {
if (socket) {
socket.send(message)
}
}
window.onload = function() {
socketUrl = document.location.protocol == "https:" ? "wss://" : "ws://"
socketUrl += document.location.hostname
2024-01-15 16:13:46 +03:00
const port = document.location.port
2022-10-27 16:14:30 +03:00
if (port) {
socketUrl += ":" + port
}
socketUrl += window.location.pathname + "ws"
socket = new WebSocket(socketUrl);
socket.onopen = socketOpen;
socket.onclose = socketClose;
socket.onerror = socketError;
socket.onmessage = function(event) {
window.execScript ? window.execScript(event.data) : window.eval(event.data);
};
};
function socketOpen() {
2022-11-01 20:13:09 +03:00
sendMessage( sessionInfo() );
2022-10-27 16:14:30 +03:00
}
function socketReopen() {
sendMessage( "reconnect{session=" + sessionID + "}" );
}
function socketReconnect() {
if (!socket) {
socket = new WebSocket(socketUrl);
socket.onopen = socketReopen;
socket.onclose = socketClose;
socket.onerror = socketError;
socket.onmessage = function(event) {
window.execScript ? window.execScript(event.data) : window.eval(event.data);
};
}
}
function socketClose(event) {
console.log("socket closed")
socket = null;
if (!event.wasClean && windowFocus) {
window.setTimeout(socketReconnect, 10000);
}
}
function socketError(error) {
console.log(error);
}
window.onfocus = function(event) {
windowFocus = true
if (!socket) {
socketReconnect()
} else {
sendMessage( "session-resume{session=" + sessionID +"}" );
}
}