// 构造推送 name 集. func buildNames(userIds []string, sessionArgs []string) (names []*Name) { for _, userId := range userIds { sessions := session.GetSessions(userId, sessionArgs) m := getUserByUid(userId) displayName := "" if nil != m { displayName = m.NickName } activeSessionIds := []string{} for _, s := range sessions { if session.SESSION_STATE_ACTIVE == s.State { activeSessionIds = append(activeSessionIds, s.Id) } } // id@user (i.e. for offline msg)发送离线消息使用 name := &Name{Id: userId, SessionId: userId /* user_id 作为 session_id */, ActiveSessionIds: activeSessionIds, Suffix: USER_SUFFIX, DisplayName: displayName} names = append(names, name) for _, s := range sessions { name := &Name{Id: userId, SessionId: s.Id, ActiveSessionIds: activeSessionIds, Suffix: USER_SUFFIX, DisplayName: displayName} names = append(names, name) } } return names }
/* baseRequest: { "uid": "", "deviceID": "", "deviceType": "", // iOS / Android "token": "" } 获取用户会话session */ func (*app) GetSession(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "Method Not Allowed", 405) return } baseRes := baseResponse{OK, ""} body := "" res := map[string]interface{}{"baseResponse": &baseRes} defer RetPWriteJSON(w, r, res, &body, time.Now()) bodyBytes, err := ioutil.ReadAll(r.Body) if err != nil { res["ret"] = ParamErr logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error()) return } body = string(bodyBytes) var args map[string]interface{} if err := json.Unmarshal(bodyBytes, &args); err != nil { baseRes.ErrMsg = err.Error() baseRes.Ret = ParamErr return } baseReq := args["baseRequest"].(map[string]interface{}) token := baseReq["token"].(string) //应用校验 _, err = getApplicationByToken(token) if nil != err { baseRes.Ret = AuthErr return } //获取用户会话列表 userId := baseReq["uid"].(string) ret := session.GetSessions(userId, []string{"all"}) res["memberList"] = ret return }