示例#1
0
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)
		}
	}
}
示例#2
0
func (m *Messaging) sendMessage(w http.ResponseWriter, to, from int, message string, chTo chan string) {
	if chTo != nil {
		fmt.Println("Undelivered")

		messageResponse := response.NewMessageResponse(0, "")

		mess := models.NewMessage(config.GetConnection())
		messagePull := mess.GetUndeliveredMessages(to)

		var newMessage models.Message
		newMessage.From = from
		newMessage.To = to
		newMessage.Body = message
		messagePull = append(messagePull, newMessage)

		mess.RemoveUndeliveredMessages(to)

		messageResponse.Data = messagePull

		fmt.Println("chan" + string(to) + " exists")

		fmt.Println("message: " + message)

		jsonResult, err := json.Marshal(messageResponse)

		if err == nil {
			chTo <- string(jsonResult)
		}
	} else {
		messageDB := models.NewMessage(config.GetConnection())
		messageDB.Body = message
		messageDB.From = from
		messageDB.To = to
		messageDB.CreateDate = time.Now().Local().Unix()
		messageDB.IsRead = false
		messageDB.Save()
	}
}