chore: change message type

pull/1604/head
Noah Hsu 2022-08-14 03:05:30 +08:00
parent fe0dee1196
commit 8cd05275f0
4 changed files with 27 additions and 22 deletions

View File

@ -37,9 +37,9 @@ func initDevData() {
func initDevDo() {
if flags.Dev {
go func() {
err := message.GetMessenger().WaitSend(map[string]string{
"type": "dev",
"msg": "dev mode",
err := message.GetMessenger().WaitSend(message.Message{
Type: "string",
Content: "dev mode",
}, 10)
if err != nil {
log.Debugf("%+v", err)

View File

@ -8,16 +8,16 @@ import (
"github.com/pkg/errors"
)
type Post struct {
type Http struct {
Received chan string // received messages from web
ToSend chan interface{} // messages to send to web
ToSend chan Message // messages to send to web
}
type Req struct {
Message string `json:"message" form:"message"`
}
func (p *Post) GetHandle(c *gin.Context) {
func (p *Http) GetHandle(c *gin.Context) {
select {
case message := <-p.ToSend:
common.SuccessResp(c, message)
@ -26,7 +26,7 @@ func (p *Post) GetHandle(c *gin.Context) {
}
}
func (p *Post) SendHandle(c *gin.Context) {
func (p *Http) SendHandle(c *gin.Context) {
var req Req
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
@ -36,20 +36,20 @@ func (p *Post) SendHandle(c *gin.Context) {
case p.Received <- req.Message:
common.SuccessResp(c)
default:
common.ErrorStrResp(c, "send failed", 500)
common.ErrorStrResp(c, "nowhere needed", 500)
}
}
func (p *Post) Send(data interface{}) error {
func (p *Http) Send(message Message) error {
select {
case p.ToSend <- data:
case p.ToSend <- message:
return nil
default:
return errors.New("send failed")
}
}
func (p *Post) Receive() (string, error) {
func (p *Http) Receive() (string, error) {
select {
case message := <-p.Received:
return message, nil
@ -58,16 +58,16 @@ func (p *Post) Receive() (string, error) {
}
}
func (p *Post) WaitSend(data interface{}, d int) error {
func (p *Http) WaitSend(message Message, d int) error {
select {
case p.ToSend <- data:
case p.ToSend <- message:
return nil
case <-time.After(time.Duration(d) * time.Second):
return errors.New("send timeout")
}
}
func (p *Post) WaitReceive(d int) (string, error) {
func (p *Http) WaitReceive(d int) (string, error) {
select {
case message := <-p.Received:
return message, nil
@ -76,7 +76,7 @@ func (p *Post) WaitReceive(d int) (string, error) {
}
}
var PostInstance = &Post{
var HttpInstance = &Http{
Received: make(chan string),
ToSend: make(chan interface{}),
ToSend: make(chan Message),
}

View File

@ -1,12 +1,17 @@
package message
type Message struct {
Type string `json:"type"`
Content interface{} `json:"content"`
}
type Messenger interface {
Send(interface{}) error
Send(Message) error
Receive() (string, error)
WaitSend(interface{}, int) error
WaitSend(Message, int) error
WaitReceive(int) (string, error)
}
func GetMessenger() Messenger {
return PostInstance
return HttpInstance
}

View File

@ -101,8 +101,8 @@ func admin(g *gin.RouterGroup) {
task.POST("/copy/clear_done", handles.ClearDoneCopyTasks)
ms := g.Group("/message")
ms.GET("/get", message.PostInstance.GetHandle)
ms.POST("/send", message.PostInstance.SendHandle)
ms.POST("/get", message.HttpInstance.GetHandle)
ms.POST("/send", message.HttpInstance.SendHandle)
}
func fs(g *gin.RouterGroup) {