Update qemu/static/js/vm_control.js
Browse files- qemu/static/js/vm_control.js +116 -116
qemu/static/js/vm_control.js
CHANGED
|
@@ -1,116 +1,116 @@
|
|
| 1 |
-
class VMController {
|
| 2 |
-
constructor() {
|
| 3 |
-
this.ws = null;
|
| 4 |
-
this.vnc = null;
|
| 5 |
-
this.setupWebSocket();
|
| 6 |
-
this.setupControls();
|
| 7 |
-
this.log('VM Controller initialized');
|
| 8 |
-
}
|
| 9 |
-
|
| 10 |
-
setupWebSocket() {
|
| 11 |
-
this.ws = new WebSocket(`
|
| 12 |
-
|
| 13 |
-
this.ws.onopen = () => {
|
| 14 |
-
this.log('Connected to VM server');
|
| 15 |
-
};
|
| 16 |
-
|
| 17 |
-
this.ws.onmessage = (event) => {
|
| 18 |
-
const msg = JSON.parse(event.data);
|
| 19 |
-
this.handleMessage(msg);
|
| 20 |
-
};
|
| 21 |
-
|
| 22 |
-
this.ws.onclose = () => {
|
| 23 |
-
this.log('Disconnected from VM server');
|
| 24 |
-
// Try to reconnect
|
| 25 |
-
setTimeout(() => this.setupWebSocket(), 5000);
|
| 26 |
-
};
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
setupControls() {
|
| 30 |
-
// Installation
|
| 31 |
-
document.getElementById('installBtn').onclick = () => {
|
| 32 |
-
const isoUrl = document.getElementById('isoUrl').value;
|
| 33 |
-
if (isoUrl) {
|
| 34 |
-
this.sendMessage({
|
| 35 |
-
type: 'install',
|
| 36 |
-
iso_url: isoUrl
|
| 37 |
-
});
|
| 38 |
-
}
|
| 39 |
-
};
|
| 40 |
-
|
| 41 |
-
// VM Controls
|
| 42 |
-
document.getElementById('bootBtn').onclick = () => {
|
| 43 |
-
this.sendMessage({ type: 'boot' });
|
| 44 |
-
};
|
| 45 |
-
|
| 46 |
-
document.getElementById('shutdownBtn').onclick = () => {
|
| 47 |
-
this.sendMessage({ type: 'shutdown' });
|
| 48 |
-
};
|
| 49 |
-
|
| 50 |
-
// Display Controls
|
| 51 |
-
document.getElementById('resolution').onchange = (e) => {
|
| 52 |
-
if (this.vnc) {
|
| 53 |
-
const [width, height] = e.target.value.split(',').map(Number);
|
| 54 |
-
this.vnc.resizeSession(width, height);
|
| 55 |
-
}
|
| 56 |
-
};
|
| 57 |
-
|
| 58 |
-
document.getElementById('fullscreenBtn').onclick = () => {
|
| 59 |
-
if (this.vnc) {
|
| 60 |
-
this.vnc.requestFullscreen();
|
| 61 |
-
}
|
| 62 |
-
};
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
handleMessage(msg) {
|
| 66 |
-
switch(msg.type) {
|
| 67 |
-
case 'vnc_info':
|
| 68 |
-
this.connectVNC(msg.port);
|
| 69 |
-
break;
|
| 70 |
-
case 'error':
|
| 71 |
-
this.log('Error: ' + msg.message);
|
| 72 |
-
break;
|
| 73 |
-
case 'status':
|
| 74 |
-
this.log(msg.message);
|
| 75 |
-
break;
|
| 76 |
-
}
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
connectVNC(port) {
|
| 80 |
-
// Initialize noVNC connection
|
| 81 |
-
this.vnc = new RFB(
|
| 82 |
-
document.getElementById(
|
| 83 |
-
`
|
| 84 |
-
);
|
| 85 |
-
|
| 86 |
-
this.vnc.addEventListener('connect', () => {
|
| 87 |
-
this.log('Connected to VM display');
|
| 88 |
-
});
|
| 89 |
-
|
| 90 |
-
this.vnc.addEventListener('disconnect', () => {
|
| 91 |
-
this.log('Disconnected from VM display');
|
| 92 |
-
});
|
| 93 |
-
|
| 94 |
-
// Set initial resolution
|
| 95 |
-
const [width, height] = document.getElementById('resolution').value.split(',').map(Number);
|
| 96 |
-
this.vnc.resizeSession(width, height);
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
sendMessage(msg) {
|
| 100 |
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
| 101 |
-
this.ws.send(JSON.stringify(msg));
|
| 102 |
-
}
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
log(message) {
|
| 106 |
-
const logEl = document.getElementById('log');
|
| 107 |
-
const timestamp = new Date().toISOString();
|
| 108 |
-
logEl.innerHTML += `[${timestamp}] ${message}\n`;
|
| 109 |
-
logEl.scrollTop = logEl.scrollHeight;
|
| 110 |
-
}
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
// Initialize controller when page loads
|
| 114 |
-
window.addEventListener('load', () => {
|
| 115 |
-
window.vmController = new VMController();
|
| 116 |
-
});
|
|
|
|
| 1 |
+
class VMController {
|
| 2 |
+
constructor() {
|
| 3 |
+
this.ws = null;
|
| 4 |
+
this.vnc = null;
|
| 5 |
+
this.setupWebSocket();
|
| 6 |
+
this.setupControls();
|
| 7 |
+
this.log('VM Controller initialized');
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
setupWebSocket() {
|
| 11 |
+
this.ws = new WebSocket(`wss://${window.location.host}/ws/vm`);
|
| 12 |
+
|
| 13 |
+
this.ws.onopen = () => {
|
| 14 |
+
this.log('Connected to VM server');
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
this.ws.onmessage = (event) => {
|
| 18 |
+
const msg = JSON.parse(event.data);
|
| 19 |
+
this.handleMessage(msg);
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
this.ws.onclose = () => {
|
| 23 |
+
this.log('Disconnected from VM server');
|
| 24 |
+
// Try to reconnect
|
| 25 |
+
setTimeout(() => this.setupWebSocket(), 5000);
|
| 26 |
+
};
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
setupControls() {
|
| 30 |
+
// Installation
|
| 31 |
+
document.getElementById('installBtn').onclick = () => {
|
| 32 |
+
const isoUrl = document.getElementById('isoUrl').value;
|
| 33 |
+
if (isoUrl) {
|
| 34 |
+
this.sendMessage({
|
| 35 |
+
type: 'install',
|
| 36 |
+
iso_url: isoUrl
|
| 37 |
+
});
|
| 38 |
+
}
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
// VM Controls
|
| 42 |
+
document.getElementById('bootBtn').onclick = () => {
|
| 43 |
+
this.sendMessage({ type: 'boot' });
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
document.getElementById('shutdownBtn').onclick = () => {
|
| 47 |
+
this.sendMessage({ type: 'shutdown' });
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
// Display Controls
|
| 51 |
+
document.getElementById('resolution').onchange = (e) => {
|
| 52 |
+
if (this.vnc) {
|
| 53 |
+
const [width, height] = e.target.value.split(',').map(Number);
|
| 54 |
+
this.vnc.resizeSession(width, height);
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
document.getElementById('fullscreenBtn').onclick = () => {
|
| 59 |
+
if (this.vnc) {
|
| 60 |
+
this.vnc.requestFullscreen();
|
| 61 |
+
}
|
| 62 |
+
};
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
handleMessage(msg) {
|
| 66 |
+
switch(msg.type) {
|
| 67 |
+
case 'vnc_info':
|
| 68 |
+
this.connectVNC(msg.port);
|
| 69 |
+
break;
|
| 70 |
+
case 'error':
|
| 71 |
+
this.log('Error: ' + msg.message);
|
| 72 |
+
break;
|
| 73 |
+
case 'status':
|
| 74 |
+
this.log(msg.message);
|
| 75 |
+
break;
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
connectVNC(port) {
|
| 80 |
+
// Initialize noVNC connection
|
| 81 |
+
this.vnc = new RFB(
|
| 82 |
+
document.getElementById("vncDisplay"),
|
| 83 |
+
`wss://${window.location.hostname}:${port}/websockify`
|
| 84 |
+
);
|
| 85 |
+
|
| 86 |
+
this.vnc.addEventListener('connect', () => {
|
| 87 |
+
this.log('Connected to VM display');
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
this.vnc.addEventListener('disconnect', () => {
|
| 91 |
+
this.log('Disconnected from VM display');
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
// Set initial resolution
|
| 95 |
+
const [width, height] = document.getElementById('resolution').value.split(',').map(Number);
|
| 96 |
+
this.vnc.resizeSession(width, height);
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
sendMessage(msg) {
|
| 100 |
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
| 101 |
+
this.ws.send(JSON.stringify(msg));
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
log(message) {
|
| 106 |
+
const logEl = document.getElementById('log');
|
| 107 |
+
const timestamp = new Date().toISOString();
|
| 108 |
+
logEl.innerHTML += `[${timestamp}] ${message}\n`;
|
| 109 |
+
logEl.scrollTop = logEl.scrollHeight;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Initialize controller when page loads
|
| 114 |
+
window.addEventListener('load', () => {
|
| 115 |
+
window.vmController = new VMController();
|
| 116 |
+
});
|