websocket: add SendAll and SendSession to HandlerService
This commit is contained in:
parent
5c20ee9561
commit
ce7ba16090
|
@ -2,6 +2,8 @@ package websocket
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// MessageHandleFunc for handling messages
|
||||
|
@ -40,6 +42,22 @@ func (ws *WebsocketHandlerService) SetHandler(subject string, f MessageHandleFun
|
|||
ws.handlers[subject] = f
|
||||
}
|
||||
|
||||
// SendAll see Server.SendAll
|
||||
func (ws *WebsocketHandlerService) SendAll(msg *Message) {
|
||||
if server := ws.server; server != nil {
|
||||
server.SendAll(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// SendSession see message to all connection of one session
|
||||
func (ws *WebsocketHandlerService) SendSession(id uuid.UUID, msg *Message) {
|
||||
if server := ws.server; server != nil {
|
||||
if mgmt := server.sessionManager; mgmt != nil {
|
||||
mgmt.Send(id, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Listen on net/http server at `path` and start running handling
|
||||
func (ws *WebsocketHandlerService) Listen(path string) {
|
||||
http.HandleFunc(path, ws.server.Handler)
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -39,4 +41,7 @@ func TestHandler(t *testing.T) {
|
|||
}
|
||||
chanMsg <- &Message{Subject: "mist", Body: "unexpected"}
|
||||
wg.Wait()
|
||||
|
||||
handlerService.SendAll(&Message{Subject: "dummy", Body: "100% maybe"})
|
||||
handlerService.SendSession(uuid.New(), &Message{Subject: "dummy", Body: "100% maybe"})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue