Skip to content

Commit

Permalink
#8, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 16, 2014
1 parent 1d08ed8 commit bda5573
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
29 changes: 26 additions & 3 deletions app/push.go
Expand Up @@ -8,6 +8,7 @@ import (
"time"

myrpc "github.com/EPICPaaS/appmsgsrv/rpc"
"github.com/EPICPaaS/appmsgsrv/session"
"github.com/golang/glog"
)

Expand Down Expand Up @@ -155,6 +156,16 @@ func (*device) Push(w http.ResponseWriter, r *http.Request) {
fromUserID := fromUserName[:strings.Index(fromUserName, "@")]
toUserName := msg["toUserName"].(string)
toUserID := toUserName[:strings.Index(toUserName, "@")]
sessionArgs := []string{}
_, exists := args["sessions"]
if !exists {
// 不存在参数的话默认为 all
sessionArgs = append(sessionArgs, "all")
} else {
for _, arg := range args["sessions"].([]interface{}) {
sessionArgs = append(sessionArgs, arg.(string))
}
}

if strings.HasSuffix(toUserName, USER_SUFFIX) { // 如果是推人
m := getUserByUid(fromUserID)
Expand Down Expand Up @@ -186,7 +197,7 @@ func (*device) Push(w http.ResponseWriter, r *http.Request) {
}

// 获取推送目标用户 Name 集
toUserNames, _ := getToUserNames(toUserName)
toUserNames, _ := getToUserNames(toUserName, sessionArgs)

// 推送分发
for _, userName := range toUserNames {
Expand Down Expand Up @@ -251,7 +262,9 @@ func push(key string, msgBytes []byte, expire int) int {
}

// 根据 toUserName 获得最终推送的 name 集.
func getToUserNames(toUserName string) (userNames []string, pushType string) {
func getToUserNames(toUserName string, sessionArgs []string) (userNames []string, pushType string) {
toUserId := toUserName[:strings.Index(toUserName, "@")]

if strings.HasSuffix(toUserName, QUN_SUFFIX) { // 群推
qunId := toUserName[:len(toUserName)-len(QUN_SUFFIX)]

Expand Down Expand Up @@ -293,7 +306,17 @@ func getToUserNames(toUserName string) (userNames []string, pushType string) {

return userNames, TENANT_SUFFIX
} else if strings.HasSuffix(toUserName, USER_SUFFIX) { // 用户推
return []string{toUserName}, USER_SUFFIX
// 目前只有用户推的时候做会话处理
sessions := session.GetSessions(toUserId, sessionArgs)
userNames := []string{}
for _, session := range sessions {
userNames = append(userNames, toUserId+"_"+session.Id+USER_SUFFIX)
}

// 离线消息使用 id@user
userNames = append(userNames, toUserId+USER_SUFFIX)

return userNames, USER_SUFFIX
} else if strings.HasSuffix(toUserName, APP_SUFFIX) { // 应用推
glog.Warningf("应用推需要走单独的接口")
return []string{}, APP_SUFFIX
Expand Down
18 changes: 16 additions & 2 deletions session/session.go
Expand Up @@ -7,8 +7,9 @@ import (
)

const (
SESSION_STATE_ACTIVE = "active"
SESSION_STATE_INACTIVE = "inactive"
SESSION_STATE_ACTIVE = "active"
SESSION_STATE_INACTIVE = "inactive"

INSERT_SESSION = "INSERT INTO `session`(`id`,`type`,`user_id`,`state`,`created`,`updated`) VALUES (?,?,?,?,?,?) "
DELETE_SESSION_BYID = "DELETE FROM `session` WHERE `id`=?"
DELETE_SESSION_BYUSERID = "DELETE FROM `session` WHERE `user_id`=?"
Expand All @@ -30,6 +31,19 @@ type Session struct {
//一个星期扫描一次
var ScanSessionTime = time.NewTicker(168 * time.Hour)

// 根据 args 参数获取用户 uid 的会话集.
//
// args 参数:
// 1. ["all"] 表示获取该用户所有的会话
// 2. ["xxx1", "xxx2"] 表示获取该用户 xxx1、xxx2 会话
// 3. ["active"] 表示获取该用户的所有激活的会话
// 4. [inactive"] 表示获取该用户的所有非激活的会话
func GetSessions(uid string, args []string) []*Session {
ret := []*Session{}

return ret
}

//创建会话session记录
func CreatSession(session *Session) bool {

Expand Down

0 comments on commit bda5573

Please sign in to comment.