func (m *Messaging) notifyChannels(id int) { chanels := models.GetMessageChannelWrapper().GetChannels() profileO := models.NewProfile(config.GetConnection()) profile := profileO.GetById(id) fmt.Println(profile.Id) if profile != nil && profile.Id > 0 { newOnlineFriendResponse := response.NewOnlineFriendResponse(0, "") newOnlineFriendResponse.Data = profile jsonResult, err := json.Marshal(newOnlineFriendResponse) fmt.Println(string(jsonResult)) if err == nil { for key, channel := range chanels { if key != id { channel <- string(jsonResult) } } } } }
func (m *Messaging) HandleRequest(w http.ResponseWriter, r *http.Request) { message := r.FormValue("msg") fromStr := r.FormValue("from") toStr := r.FormValue("to") if fromStr == "" { result := response.NewMessageResponse(0, "") w.WriteHeader(400) result.Result = 400 result.ResultMessage = "Who are you?" } else { from, errFrom := strconv.Atoi(fromStr) to, errTo := strconv.Atoi(toStr) if errFrom == nil { fmt.Println("Get channels") chFrom := models.GetMessageChannelWrapper().GetChannel(from, false) if chFrom == nil { m.notifyChannels(from) chFrom = models.GetMessageChannelWrapper().GetChannel(from, true) } chTo := models.GetMessageChannelWrapper().GetChannel(to, false) if message != "" { m.sendMessage(w, to, from, message, chTo) } else { m.updateConnection(w, from, chFrom) } } else { if errFrom != nil { fmt.Println(errFrom.Error()) } if errTo != nil { fmt.Println(errTo.Error()) } w.WriteHeader(400) } } }
/** * Get online friends */ func (f *Friends) GetOnlineUsers(w http.ResponseWriter, req *http.Request) { result := response.NewFriendsReponse(0, "") onlineKeys := models.GetMessageChannelWrapper().GetChannelKeys() result.Data = models.NewProfile(config.GetConnection()).GetUsersByIds(onlineKeys) jsonResult, err := json.Marshal(result) if err != nil { w.WriteHeader(500) } else { w.Write(jsonResult) } }