-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ func InitData() { | |
initSettings() | ||
if args.Dev { | ||
initDevData() | ||
initDevDo() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package message | ||
|
||
type Messenger interface { | ||
Send(string, interface{}) error | ||
WaitSend(string, interface{}) error | ||
Receive(string) (string, error) | ||
WaitReceive(string) (string, error) | ||
Send(interface{}) error | ||
Receive() (string, error) | ||
WaitSend(interface{}, int) error | ||
WaitReceive(int) (string, error) | ||
} | ||
|
||
func GetMessenger() Messenger { | ||
return PostInstance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package message | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/server/common" | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
"time" | ||
) | ||
|
||
type Post struct { | ||
Received chan string // received messages from web | ||
ToSend chan interface{} // messages to send to web | ||
} | ||
|
||
type Req struct { | ||
Message string `json:"message" form:"message"` | ||
} | ||
|
||
func (p *Post) GetHandle(c *gin.Context) { | ||
select { | ||
case message := <-p.ToSend: | ||
common.SuccessResp(c, message) | ||
default: | ||
common.ErrorStrResp(c, "no message", 404) | ||
} | ||
} | ||
|
||
func (p *Post) SendHandle(c *gin.Context) { | ||
var req Req | ||
if err := c.ShouldBind(&req); err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} | ||
select { | ||
case p.Received <- req.Message: | ||
common.SuccessResp(c) | ||
default: | ||
common.ErrorStrResp(c, "send failed", 500) | ||
} | ||
} | ||
|
||
func (p *Post) Send(data interface{}) error { | ||
select { | ||
case p.ToSend <- data: | ||
return nil | ||
default: | ||
return errors.New("send failed") | ||
} | ||
} | ||
|
||
func (p *Post) Receive() (string, error) { | ||
select { | ||
case message := <-p.Received: | ||
return message, nil | ||
default: | ||
return "", errors.New("receive failed") | ||
} | ||
} | ||
|
||
func (p *Post) WaitSend(data interface{}, d int) error { | ||
select { | ||
case p.ToSend <- data: | ||
return nil | ||
case <-time.After(time.Duration(d) * time.Second): | ||
return errors.New("send timeout") | ||
} | ||
} | ||
|
||
func (p *Post) WaitReceive(d int) (string, error) { | ||
select { | ||
case message := <-p.Received: | ||
return message, nil | ||
case <-time.After(time.Duration(d) * time.Second): | ||
return "", errors.New("receive timeout") | ||
} | ||
} | ||
|
||
var PostInstance = &Post{ | ||
Received: make(chan string), | ||
ToSend: make(chan interface{}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package message | ||
|
||
// TODO websocket implementation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters