adjust: code structure

This commit is contained in:
2024-10-29 17:11:14 +08:00
parent dfdb6003ea
commit d991c7cac6
12 changed files with 234 additions and 192 deletions

View File

@@ -3,10 +3,7 @@ package server
import (
"embed"
"html/template"
c "live-streamer/config"
"live-streamer/logger"
"live-streamer/streamer"
mywebsocket "live-streamer/websocket"
"net/http"
"sync"
"time"
@@ -26,13 +23,17 @@ var upgrader = websocket.Upgrader{
},
}
type InputFunc func(mywebsocket.RequestType)
type Server struct {
addr string
dealInputFunc InputFunc
clients map[string]*Client
mu sync.Mutex
addr string
clients map[string]*Client
mu sync.Mutex
option *Option
log *zap.Logger
streamer *streamer.Streamer
}
type Option struct {
AuthToken string
}
type Client struct {
@@ -45,20 +46,16 @@ type Client struct {
var (
GlobalServer *Server
config *c.Config
log *zap.Logger
log *zap.Logger
)
func init() {
config = c.GlobalConfig
log = logger.GlobalLogger
}
func NewServer(addr string, dealInputFunc InputFunc) {
func NewServer(addr string, streamer *streamer.Streamer, log *zap.Logger, option *Option) {
GlobalServer = &Server{
addr: addr,
dealInputFunc: dealInputFunc,
clients: make(map[string]*Client),
addr: addr,
option: option,
clients: make(map[string]*Client),
log: log,
streamer: streamer,
}
}
@@ -71,7 +68,7 @@ func (s *Server) Run() {
}
router.SetHTMLTemplate(tpl)
router.GET("/ws", AuthMiddleware(), s.handleWebSocket)
router.GET("/ws", s.AuthMiddleware(), s.handleWebSocket)
router.GET(
"/", func(c *gin.Context) {
c.HTML(200, "index.html", nil)
@@ -120,11 +117,11 @@ func (s *Server) handleWebSocket(c *gin.Context) {
go func() {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
s.Broadcast(mywebsocket.Date{
s.Broadcast(Data{
Timestamp: time.Now().UnixMilli(),
CurrentVideoPath: streamer.GlobalStreamer.GetCurrentVideoPath(),
VideoList: streamer.GlobalStreamer.GetVideoListPath(),
Output: streamer.GlobalStreamer.GetOutput(),
CurrentVideoPath: s.streamer.GetCurrentVideoPath(),
VideoList: s.streamer.GetVideoListPath(),
Output: s.streamer.GetOutput(),
})
}
}()
@@ -132,23 +129,20 @@ func (s *Server) handleWebSocket(c *gin.Context) {
for {
// recive message
client.mu.Lock()
msg := mywebsocket.Request{}
msg := Request{}
err := ws.ReadJSON(&msg)
client.mu.Unlock()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Error("websocket error", zap.Error(err))
}
break
}
s.dealInputFunc(msg.Type)
s.RequestHandler(msg.Type)
}
}
func AuthMiddleware() gin.HandlerFunc {
func (s *Server) AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if config.Server.Token == "" ||
c.Query("token") == config.Server.Token {
if s.option.AuthToken == "" ||
c.Query("token") == s.option.AuthToken {
c.Next()
} else {
c.AbortWithStatus(http.StatusUnauthorized)
@@ -156,7 +150,7 @@ func AuthMiddleware() gin.HandlerFunc {
}
}
func (s *Server) Broadcast(obj mywebsocket.Date) {
func (s *Server) Broadcast(obj Data) {
s.mu.Lock()
for _, client := range s.clients {
obj.Timestamp = time.Now().UnixMilli()
@@ -167,7 +161,7 @@ func (s *Server) Broadcast(obj mywebsocket.Date) {
s.mu.Unlock()
}
func (s *Server) Single(userID string, obj mywebsocket.Date) {
func (s *Server) Single(userID string, obj Data) {
s.mu.Lock()
if client, ok := s.clients[userID]; ok {
obj.Timestamp = time.Now().UnixMilli()
@@ -178,6 +172,13 @@ func (s *Server) Single(userID string, obj mywebsocket.Date) {
s.mu.Unlock()
}
func (s *Server) Close() {
func (s *Server) RequestHandler(reqType RequestType) {
switch reqType {
case TypeStreamNextVideo:
s.streamer.Next()
case TypeStreamPrevVideo:
s.streamer.Prev()
case TypeQuit:
s.streamer.Close()
}
}

View File

@@ -368,8 +368,31 @@
<script>
let ws;
let shouldAutoScroll = true;
let isConnecting = false;
let reconnectTimer = null;
function connectWebSocket() {
if (isConnecting || (ws && ws.readyState === WebSocket.CONNECTING)) {
console.log("WebSocket connection already in progress");
return;
}
if (ws && ws.readyState === WebSocket.OPEN) {
console.log("WebSocket already connected");
return;
}
if (ws) {
ws.close();
ws = null;
}
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
isConnecting = true;
const token = document.getElementById("token-input").value;
const wsProtocol =
window.location.protocol === "https:" ? "wss:" : "ws:";
@@ -378,6 +401,7 @@
ws.onopen = function () {
console.log("Connected to WebSocket");
isConnecting = false;
setStoredToken(token);
document.getElementById("token-screen").style.display = "none";
document.querySelector(".container-fluid").style.display = "flex";
@@ -404,15 +428,21 @@
};
ws.onerror = function () {
isConnecting = false;
document.getElementById("token-error").style.display = "block";
};
ws.onclose = function () {
isConnecting = false;
console.log("Disconnected from WebSocket");
document.getElementById("status").textContent =
"WebSocket Status: Disconnected";
document.getElementById("status").classList.remove("connected");
setTimeout(connectWebSocket, 3000);
if (reconnectTimer) {
clearTimeout(reconnectTimer);
}
reconnectTimer = setTimeout(connectWebSocket, 3000);
};
}
@@ -480,6 +510,11 @@
sendWs("Quit");
if (ws) {
ws.close();
ws = null;
}
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
}
};

20
server/websocket.go Executable file
View File

@@ -0,0 +1,20 @@
package server
type RequestType string
const (
TypeStreamNextVideo RequestType = "StreamNextVideo"
TypeStreamPrevVideo RequestType = "StreamPrevVideo"
TypeQuit RequestType = "Quit"
)
type Request struct {
Type RequestType `json:"type"`
}
type Data struct {
Timestamp int64 `json:"timestamp"`
CurrentVideoPath string `json:"currentVideoPath"`
VideoList []string `json:"videoList"`
Output string `json:"output"`
}