overflow_gateway_webapp/servlet/webapp/subscriber_handlers.go
crusader 0c7a5e3330 ing
2018-03-23 01:10:12 +09:00

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()
}