live-streamer/server/static/index.html
nite07 369b658d60 fix: front-end not save token
fix: sometimes not auto stream next video
2024-10-28 15:52:02 +08:00

489 lines
12 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Live Streamer</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
rel="stylesheet"
/>
<style>
body,
html {
height: 100vh;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
background-color: #f0f2f5;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden;
}
#token-screen {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(135deg, #6e8efb, #4a6cf7);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.token-container {
background: white;
padding: 30px;
border-radius: 15px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 400px;
}
.token-container h2 {
margin-bottom: 20px;
color: #333;
text-align: center;
}
.token-input-group {
margin-bottom: 20px;
}
.token-input-group input {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 16px;
transition: border-color 0.3s ease;
}
.token-input-group input:focus {
border-color: #4a6cf7;
outline: none;
}
#token-error {
color: #dc3545;
font-size: 14px;
margin-top: 10px;
display: none;
}
.container-fluid {
flex: 1;
display: flex;
flex-direction: column;
padding: 15px;
height: 100%;
gap: 15px;
overflow: hidden;
}
.header {
flex: 0 0 auto;
background: linear-gradient(135deg, #6e8efb, #4a6cf7);
color: white;
padding: 15px 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.header h2 {
margin: 0;
font-weight: 600;
}
#status {
flex: 0 0 auto;
background-color: white;
padding: 10px 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
#output-container {
flex: 1;
min-height: 100px;
display: flex;
flex-direction: column;
}
#messages {
flex: 1;
height: auto !important;
resize: none;
border-radius: 8px;
padding: 15px;
font-family: "Consolas", monospace;
font-size: 0.9rem;
line-height: 1.5;
background-color: #2b2b2b;
color: #e0e0e0;
border: none;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
#app-container {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
gap: 15px;
min-height: 200px;
}
#current-video {
flex: 0 0 60px;
background-color: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
display: flex;
align-items: center;
}
.bottom-section {
flex: 1;
display: flex;
gap: 15px;
min-height: 160px;
max-height: 300px;
}
#control-panel {
flex: 0 0 150px;
display: flex;
flex-direction: column;
gap: 10px;
padding: 15px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.btn {
border-radius: 6px;
font-weight: 500;
text-transform: uppercase;
font-size: 0.85rem;
letter-spacing: 0.5px;
padding: 8px 15px;
transition: all 0.3s ease;
}
.btn-primary {
background: linear-gradient(135deg, #6e8efb, #4a6cf7);
border: none;
}
.btn-primary:hover {
background: linear-gradient(135deg, #5d7df9, #3959f5);
transform: translateY(-1px);
}
.btn-danger {
background: linear-gradient(135deg, #ff6b6b, #ee5253);
border: none;
}
.btn-danger:hover {
background: linear-gradient(135deg, #ff5252, #ed4444);
transform: translateY(-1px);
}
#video-list-container {
flex: 1;
background-color: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
display: flex;
flex-direction: column;
}
#video-list {
font-weight: 600;
color: #333;
margin-bottom: 10px;
flex: 0 0 auto;
}
.list-group {
flex: 1;
overflow-y: auto;
padding-right: 5px;
margin-bottom: 0;
}
.list-group-item {
border: none;
border-radius: 6px !important;
margin-bottom: 5px;
padding: 12px 15px;
background-color: #f8f9fa;
transition: all 0.2s ease;
}
.list-group-item:last-child {
margin-bottom: 0;
}
.list-group-item:hover {
background-color: #e9ecef;
transform: translateX(5px);
}
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #555;
}
#status::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 8px;
background-color: #dc3545;
}
#status.connected::before {
background-color: #28a745;
}
@media (max-height: 600px) {
.container-fluid {
gap: 10px;
padding: 10px;
}
.header {
padding: 10px;
}
#current-video {
flex: 0 0 40px;
padding: 10px;
}
.bottom-section {
gap: 10px;
}
}
@media (max-width: 768px) {
#control-panel {
flex: 0 0 120px;
}
.btn {
padding: 6px 12px;
font-size: 0.8rem;
}
}
</style>
</head>
<body>
<div id="token-screen">
<div class="token-container">
<h2><i class="fas fa-lock me-2"></i>访问验证</h2>
<div class="token-input-group">
<input
type="password"
id="token-input"
placeholder="请输入访问令牌"
class="form-control"
/>
</div>
<button class="btn btn-primary w-100" onclick="validateToken()">
<i class="fas fa-sign-in-alt me-2"></i>验证并进入
</button>
<div id="token-error">访问令牌无效,请重试</div>
</div>
</div>
<div class="container-fluid">
<div class="header">
<h2><i class="fas fa-video me-2"></i>Live Streamer</h2>
</div>
<div id="status">WebSocket Status: Disconnected</div>
<div id="output-container">
<textarea id="messages" class="form-control" readonly>
消息区域</textarea
>
</div>
<div id="app-container">
<div id="current-video">
<i class="fas fa-play-circle me-2"></i><span>当前播放: 无</span>
</div>
<div class="bottom-section">
<div id="control-panel">
<button class="btn btn-primary" onclick="previousVideo()">
<i class="fas fa-step-backward me-2"></i>上一个
</button>
<button class="btn btn-primary" onclick="nextVideo()">
<i class="fas fa-step-forward me-2"></i>下一个
</button>
<button class="btn btn-danger" onclick="closeConnection()">
<i class="fas fa-power-off me-2"></i>关闭推流
</button>
</div>
<div id="video-list-container">
<div id="video-list"><i class="fas fa-list me-2"></i>视频列表</div>
<ul class="list-group list-group-flush">
<!-- <li class="list-group-item">
<i class="fas fa-file-video me-2"></i>Cras justo odio
</li> -->
</ul>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
let ws;
let shouldAutoScroll = true;
function connectWebSocket() {
const token = document.getElementById("token-input").value;
const wsProtocol =
window.location.protocol === "https:" ? "wss:" : "ws:";
const wsHost = window.location.host;
ws = new WebSocket(`${wsProtocol}//${wsHost}/ws?token=${token}`);
ws.onopen = function () {
console.log("Connected to WebSocket");
setStoredToken(token);
document.getElementById("token-screen").style.display = "none";
document.querySelector(".container-fluid").style.display = "flex";
document.getElementById("status").textContent =
"WebSocket Status: Connected";
document.getElementById("status").classList.add("connected");
};
ws.onmessage = function (evt) {
let obj = JSON.parse(evt.data);
messagesArea.value = obj.output;
if (shouldAutoScroll) {
messagesArea.scrollTop = messagesArea.scrollHeight;
}
document.querySelector("#current-video>span").innerHTML =
obj.currentVideoPath;
const listContainer = document.querySelector(
"#video-list-container .list-group"
);
listContainer.innerHTML = "";
obj.videoList.forEach((item) => {
listContainer.innerHTML += `<li class="list-group-item"><i class="fas fa-file-video me-2"></i>${item}</li>`;
});
};
ws.onerror = function () {
document.getElementById("token-error").style.display = "block";
};
ws.onclose = function () {
console.log("Disconnected from WebSocket");
document.getElementById("status").textContent =
"WebSocket Status: Disconnected";
document.getElementById("status").classList.remove("connected");
setTimeout(connectWebSocket, 3000);
};
}
function getStoredToken() {
return localStorage.getItem("streaming_token");
}
function setStoredToken(token) {
localStorage.setItem("streaming_token", token);
}
document.addEventListener("DOMContentLoaded", function () {
const storedToken = getStoredToken();
if (storedToken) {
document.getElementById("token-input").value = storedToken;
validateToken();
}
});
function validateToken() {
const tokenInput = document.getElementById("token-input");
if (tokenInput.value) {
connectWebSocket();
} else {
document.getElementById("token-error").style.display = "block";
}
}
document
.getElementById("token-input")
.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
validateToken();
}
});
const messagesArea = document.getElementById("messages");
messagesArea.addEventListener("scroll", function () {
const maxScrollTop =
messagesArea.scrollHeight - messagesArea.clientHeight;
if (messagesArea.scrollTop === maxScrollTop) {
shouldAutoScroll = true;
} else {
shouldAutoScroll = false;
}
});
function sendWs(type) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(`{ "type": "${type}" }`);
}
}
window.previousVideo = function () {
sendWs("StreamPrevVideo");
};
window.nextVideo = function () {
sendWs("StreamNextVideo");
};
window.closeConnection = function () {
if (confirm("确定要关闭服务器吗?")) {
sendWs("Quit");
if (ws) {
ws.close();
}
}
};
</script>
</body>
</html>