62 lines
1.2 KiB
Go
62 lines
1.2 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"
|
|
)
|
|
|
|
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)
|
|
}
|