overflow_gateway_webapp/servlet/webapp/subscriber_handlers.go

69 lines
1.9 KiB
Go
Raw Normal View History

2017-12-15 09:28:53 +00:00
package webapp
2017-11-14 03:45:48 +00:00
2017-11-14 10:10:28 +00:00
import (
2018-03-19 08:04:48 +00:00
"git.loafle.net/commons_go/logging"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
2017-11-14 10:10:28 +00:00
"git.loafle.net/commons_go/websocket_fasthttp/websocket"
2018-03-19 08:04:48 +00:00
oogw "git.loafle.net/overflow/overflow_gateway_websocket"
2017-11-14 10:10:28 +00:00
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) {
2018-03-19 08:04:48 +00:00
switch message.TargetType {
case oos.MEMBER:
for _, uid := range message.Targets {
sockets := sh.getMemberSockets(uid)
if nil == sockets {
continue
}
for _, soc := range sockets {
2018-03-22 16:10:12 +00:00
if err := soc.WriteMessageUseWriter(websocket.TextMessage, message.Message); nil != err {
logging.Logger().Errorf("OnSubscribe: write error %v", err)
}
2018-03-19 08:04:48 +00:00
}
}
case oos.MEMBER_SESSION:
for _, sid := range message.Targets {
if soc := sh.GetSocket(sid); nil != soc {
2018-03-22 16:10:12 +00:00
if err := soc.WriteMessageUseWriter(websocket.TextMessage, message.Message); nil != err {
logging.Logger().Errorf("OnSubscribe: write error %v", err)
}
2018-03-19 08:04:48 +00:00
}
}
default:
2018-03-21 11:46:47 +00:00
logging.Logger().Warnf("[WEBAPP] SubscriberHandler: Unknown TargetType %s", message.TargetType)
2018-03-19 08:04:48 +00:00
}
}
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)
2017-11-14 10:10:28 +00:00
}
}
2018-03-19 08:04:48 +00:00
return sockets
2017-11-14 10:10:28 +00:00
}
func (sh *SubscriberHandlers) Validate() {
sh.SubscriberHandlers.Validate()
}