52 lines
978 B
Go
52 lines
978 B
Go
package entitys
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/gofiber/websocket/v2"
|
|
)
|
|
|
|
type Response string
|
|
|
|
const (
|
|
ResponseJson Response = "json"
|
|
ResponseLoading Response = "loading"
|
|
ResponseEnd Response = "end"
|
|
ResponseStream Response = "stream"
|
|
ResponseText Response = "txt"
|
|
ResponseImg Response = "img"
|
|
ResponseFile Response = "file"
|
|
ResponseErr Response = "error"
|
|
ResponseLog Response = "log"
|
|
)
|
|
|
|
type ResponseData struct {
|
|
Done bool
|
|
Content string
|
|
Type Response
|
|
}
|
|
|
|
func MsgSet(msgType Response, 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 ResponseData) error {
|
|
jsonByte, _ := json.Marshal(msg)
|
|
|
|
return c.WriteMessage(websocket.TextMessage, jsonByte)
|
|
}
|
|
|
|
func MsgSendByte(c *websocket.Conn, msg []byte) {
|
|
|
|
_ = c.WriteMessage(websocket.TextMessage, msg)
|
|
}
|