From 5277fbfa3b02c51199a9e872b30e7a8dd22ea56e Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Thu, 16 Feb 2023 01:08:35 +0800 Subject: [PATCH] Improve --- file/FileWR.go | 4 +++ websocket/Recoder.go | 25 ++++++++++++++-- websocket/Server.go | 69 ++++++++++++++++++++++---------------------- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/file/FileWR.go b/file/FileWR.go index 7bfe0d2..15c2cf8 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -244,6 +244,10 @@ func (t *File) Delete() error { } defer t.Unlock() + if t.IsDir() { + return os.RemoveAll(t.Config.FilePath) + } + return os.Remove(t.Config.FilePath) } diff --git a/websocket/Recoder.go b/websocket/Recoder.go index 5078727..5e703d7 100644 --- a/websocket/Recoder.go +++ b/websocket/Recoder.go @@ -92,15 +92,34 @@ func Play(filePath string, perReadSize int, maxReadSize int) (s *Server, close f f := file.New(filePath, 0, false) defer f.Close() - startT := time.Now() timer := time.NewTicker(time.Second) + defer timer.Stop() + var ( + cu float64 data []byte err error ) + s.Interface().Pull_tag(map[string]func(any) (disable bool){ + `recv`: func(a any) (disable bool) { + if d, ok := a.(Uinterface); ok { + switch string(d.Data) { + case "pause": + timer.Stop() + case "play": + timer.Reset(time.Second) + default: + cu, _ = strconv.ParseFloat(string(d.Data), 64) + } + } + return false + }, + }) + for sg.Islive() { - cu := (<-timer.C).Sub(startT).Seconds() + <-timer.C + cu += 1 for sg.Islive() { if data == nil { @@ -112,6 +131,8 @@ func Play(filePath string, perReadSize int, maxReadSize int) (s *Server, close f tIndex := bytes.Index(data, []byte{','}) if d, _ := strconv.ParseFloat(string(data[:tIndex]), 64); d > cu { break + } else if d-cu > 2 { + continue } danmuIndex := tIndex + bytes.Index(data[tIndex+2:], []byte{','}) + 3 s.Interface().Push_tag(`send`, Uinterface{ diff --git a/websocket/Server.go b/websocket/Server.go index cef76fc..839a8e6 100644 --- a/websocket/Server.go +++ b/websocket/Server.go @@ -116,40 +116,41 @@ func (t *Server) WS(w http.ResponseWriter, r *http.Request) (o chan uintptr) { return } -//how to use -// ws_mq.Pull_tag(map[string]func(interface{})(bool){ -// `recv`:func(data interface{})(bool){ -// if tmp,ok := data.(Uinterface);ok { -// log.Println(tmp.Id,string(tmp.Data)) - -// if string(tmp.Data) == `close` { -// ws_mq.Push_tag(`close`,Uinterface{//close -// Id:0,//close all connect -// }) -// //or -// // ws_mq.Push_tag(`close`,Uinterface{//close -// // Id:tmp.Id,//close this connect -// // }) -// return false -// } - -// ws_mq.Push_tag(`send`,Uinterface{//just reply -// Id:tmp.Id, -// Data:tmp.Data, -// }) -// //or -// ws_mq.Push_tag(`send`,Uinterface{//just reply -// Id:0,//send to all -// Data:tmp.Data, -// }) -// } -// return false -// }, -// `error`:func(data interface{})(bool){ -// log.Println(data) -// return false -// }, -// }) +// how to use +// +// ws_mq.Pull_tag(map[string]func(interface{})(bool){ +// `recv`:func(data interface{})(bool){ +// if tmp,ok := data.(Uinterface);ok { +// log.Println(tmp.Id,string(tmp.Data)) +// +// if string(tmp.Data) == `close` { +// ws_mq.Push_tag(`close`,Uinterface{//close +// Id:0,//close all connect +// }) +// //or +// // ws_mq.Push_tag(`close`,Uinterface{//close +// // Id:tmp.Id,//close this connect +// // }) +// return false +// } +// +// ws_mq.Push_tag(`send`,Uinterface{//just reply +// Id:tmp.Id, +// Data:tmp.Data, +// }) +// //or +// ws_mq.Push_tag(`send`,Uinterface{//just reply +// Id:0,//send to all +// Data:tmp.Data, +// }) +// } +// return false +// }, +// `error`:func(data interface{})(bool){ +// log.Println(data) +// return false +// }, +// }) func (t *Server) Interface() *mq.Msgq { return t.ws_mq } -- 2.39.2