websocket: add SendAll and SendSession to HandlerService

This commit is contained in:
Martin/Geno 2018-08-24 21:51:56 +02:00
parent 5c20ee9561
commit ce7ba16090
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 23 additions and 0 deletions

View File

@ -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)

View File

@ -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"})
}