109 lines
2.1 KiB
Go
109 lines
2.1 KiB
Go
package entitys
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gofiber/websocket/v2"
|
|
)
|
|
|
|
type ResponseType string
|
|
|
|
const (
|
|
ResponseJson ResponseType = "json"
|
|
ResponseLoading ResponseType = "loading"
|
|
ResponseEnd ResponseType = "end"
|
|
ResponseStream ResponseType = "stream"
|
|
ResponseText ResponseType = "txt"
|
|
ResponseImg ResponseType = "img"
|
|
ResponseFile ResponseType = "file"
|
|
ResponseErr ResponseType = "error"
|
|
ResponseLog ResponseType = "log"
|
|
ResponseAuth ResponseType = "auth"
|
|
)
|
|
|
|
func ResLog(ch chan Response, index string, content string) {
|
|
ch <- Response{
|
|
Index: index,
|
|
Content: content,
|
|
Type: ResponseLog,
|
|
}
|
|
}
|
|
|
|
func ResStream(ch chan Response, index string, content string) {
|
|
ch <- Response{
|
|
Index: index,
|
|
Content: content,
|
|
Type: ResponseStream,
|
|
}
|
|
}
|
|
|
|
func ResJson(ch chan Response, index string, content string) {
|
|
ch <- Response{
|
|
Index: index,
|
|
Content: content,
|
|
Type: ResponseJson,
|
|
}
|
|
}
|
|
|
|
func ResEnd(ch chan Response, index string, content string) {
|
|
ch <- Response{
|
|
Index: index,
|
|
Content: content,
|
|
Type: ResponseEnd,
|
|
}
|
|
}
|
|
|
|
func ResText(ch chan Response, index string, content string) {
|
|
ch <- Response{
|
|
Index: index,
|
|
Content: content,
|
|
Type: ResponseText,
|
|
}
|
|
}
|
|
|
|
func ResLoading(ch chan Response, index string, content string) {
|
|
ch <- Response{
|
|
Index: index,
|
|
Content: content,
|
|
Type: ResponseLoading,
|
|
}
|
|
}
|
|
|
|
type ResponseData struct {
|
|
Done bool
|
|
Content string
|
|
Type ResponseType
|
|
}
|
|
|
|
type Response struct {
|
|
Content string
|
|
Type ResponseType
|
|
Index string
|
|
}
|
|
|
|
func MsgSet(msgType ResponseType, msg string, done bool) []byte {
|
|
jsonByte, err := json.Marshal(ResponseData{
|
|
Done: done,
|
|
Content: msg,
|
|
|
|
Type: msgType,
|
|
})
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return jsonByte
|
|
}
|
|
|
|
func MsgSend(c *websocket.Conn, msg Response) error {
|
|
// 检查上下文是否已取消
|
|
if msg.Type == ResponseText {
|
|
|
|
}
|
|
jsonByte, _ := json.Marshal(msg)
|
|
return c.WriteMessage(websocket.TextMessage, jsonByte)
|
|
}
|
|
|
|
func MsgSendByte(c *websocket.Conn, msg []byte) {
|
|
|
|
_ = c.WriteMessage(websocket.TextMessage, msg)
|
|
}
|