This commit is contained in:
crusader 2018-04-13 20:24:06 +09:00
parent eaa0c12de7
commit 94901d8877

View File

@ -13,7 +13,6 @@ import (
ogrs "git.loafle.net/overflow/gateway_rpc/servlet" ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
"git.loafle.net/overflow/member_gateway_rpc/subscribe" "git.loafle.net/overflow/member_gateway_rpc/subscribe"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@ -119,58 +118,74 @@ func (s *WebappServlets) OnDisconnect(servletCtx server.ServletCtx) {
} }
func (s *WebappServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeChan <-chan *ogs.Message) { func (s *WebappServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeChan <-chan *ogs.Message) {
var sessions []*ogrs.Session
LOOP:
for { for {
select { select {
case msg, ok := <-subscribeChan: case msg, ok := <-subscribeChan:
if !ok { if !ok {
return return
} }
switch msg.TargetType { switch msg.TargetType {
case ogs.MEMBER: case ogs.MEMBER:
for _, targetID := range msg.Targets { sessions = s.getMemberSessionsByTargetIDs(msg.Targets)
_sessions := s.getMemberSessions(targetID)
if nil == _sessions || 0 == len(_sessions) {
break
}
for _, _session := range _sessions {
_writeChan := _session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
if nil != _writeChan {
writeChan := _writeChan.(chan<- []byte)
writeChan <- msg.Message
}
}
}
case ogs.MEMBER_SESSION: case ogs.MEMBER_SESSION:
for _, sessionID := range msg.Targets { sessions = s.getMemberSessions(msg.Targets)
_session, ok := s.sessions.Load(sessionID) default:
if !ok { logging.Logger().Warnf("Subscriber: Unknown TargetType %s", msg.TargetType)
logging.Logger().Debugf("Client[%s] is not exist", sessionID) continue LOOP
break
} }
session := _session.(*ogrs.Session)
if nil == sessions || 0 == len(sessions) {
continue LOOP
}
for _, session := range sessions {
_writeChan := session.ServletCtx.GetAttribute(og.SessionWriteChanKey) _writeChan := session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
if nil != _writeChan { if nil != _writeChan {
writeChan := _writeChan.(chan<- []byte) writeChan := _writeChan.(chan<- []byte)
writeChan <- msg.Message writeChan <- *msg.Message
} }
} }
default:
logging.Logger().Warnf("Subscriber: Unknown TargetType %s", msg.TargetType)
}
} }
} }
} }
func (s *WebappServlets) getMemberSessions(targetID string) []*ogrs.Session { func (s *WebappServlets) getMemberSessions(sessionIDs []string) []*ogrs.Session {
var sessions []*ogrs.Session var sessions []*ogrs.Session
if nil == sessionIDs || 0 == len(sessionIDs) {
return sessions
}
for _, sessionID := range sessionIDs {
session, ok := s.sessions.Load(sessionID)
if ok {
sessions = append(sessions, session.(*ogrs.Session))
}
}
return sessions
}
func (s *WebappServlets) getMemberSessionsByTargetIDs(targetIDs []string) []*ogrs.Session {
var sessions []*ogrs.Session
if nil == targetIDs || 0 == len(targetIDs) {
return sessions
}
s.sessions.Range(func(k, v interface{}) bool { s.sessions.Range(func(k, v interface{}) bool {
session := v.(*ogrs.Session) session := v.(*ogrs.Session)
for _, targetID := range targetIDs {
if session.TargetID == targetID { if session.TargetID == targetID {
sessions = append(sessions, session) sessions = append(sessions, session)
break
} }
}
return true return true
}) })