overflow_gateway_webapp/servlet/webapp/subscriber_handlers.go
crusader f854dbf21d ing
2018-03-21 20:46:47 +09:00

65 lines
1.7 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 {
soc.WriteMessage(websocket.TextMessage, message.Message)
}
}
case oos.MEMBER_SESSION:
for _, sid := range message.Targets {
if soc := sh.GetSocket(sid); nil != soc {
soc.WriteMessage(websocket.TextMessage, message.Message)
}
}
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()
}