package webapp import ( "fmt" "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().Warn(fmt.Sprintf("[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() }