69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package webapp
|
|
|
|
import (
|
|
"git.loafle.net/commons_go/logging"
|
|
cwf "git.loafle.net/commons_go/websocket_fasthttp"
|
|
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
|
|
oogw "git.loafle.net/overflow/overflow_gateway_websocket"
|
|
oogws "git.loafle.net/overflow/overflow_gateway_websocket/servlet"
|
|
oogwsub "git.loafle.net/overflow/overflow_gateway_websocket/subscribe"
|
|
oos "git.loafle.net/overflow/overflow_subscriber"
|
|
)
|
|
|
|
func newSubscriberHandler(servletHandler oogws.ServletHandler) oogwsub.SubscriberHandler {
|
|
sh := &SubscriberHandlers{}
|
|
sh.ServletHandler = servletHandler
|
|
sh.Channel = cfg.Entry
|
|
|
|
return sh
|
|
}
|
|
|
|
type SubscriberHandlers struct {
|
|
oogwsub.SubscriberHandlers
|
|
}
|
|
|
|
func (sh *SubscriberHandlers) OnSubscribe(channel string, message oos.SubscribeMessage) {
|
|
switch message.TargetType {
|
|
case oos.MEMBER:
|
|
for _, uid := range message.Targets {
|
|
sockets := sh.getMemberSockets(uid)
|
|
if nil == sockets {
|
|
continue
|
|
}
|
|
|
|
for _, soc := range sockets {
|
|
if err := soc.WriteMessageUseWriter(websocket.TextMessage, message.Message); nil != err {
|
|
logging.Logger().Errorf("OnSubscribe: write error %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
case oos.MEMBER_SESSION:
|
|
for _, sid := range message.Targets {
|
|
if soc := sh.GetSocket(sid); nil != soc {
|
|
if err := soc.WriteMessageUseWriter(websocket.TextMessage, message.Message); nil != err {
|
|
logging.Logger().Errorf("OnSubscribe: write error %v", err)
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
logging.Logger().Warnf("[WEBAPP] SubscriberHandler: Unknown TargetType %s", message.TargetType)
|
|
}
|
|
|
|
}
|
|
|
|
func (sh *SubscriberHandlers) getMemberSockets(targetID string) []cwf.Socket {
|
|
var sockets []cwf.Socket
|
|
|
|
for _, socket := range sh.GetSockets() {
|
|
if socket.Context().GetAttribute(oogw.TargetIDKey).(string) == targetID {
|
|
sockets = append(sockets, socket)
|
|
}
|
|
}
|
|
return sockets
|
|
}
|
|
|
|
func (sh *SubscriberHandlers) Validate() {
|
|
sh.SubscriberHandlers.Validate()
|
|
}
|