Beispiel #1
0
func (api *API) handlePostMessage(res http.ResponseWriter, req *http.Request) error {
	ctx := appengine.NewContext(req)

	var message struct{ Text string }
	err := json.NewDecoder(req.Body).Decode(&message)
	if err != nil {
		return err
	}

	query := datastore.NewQuery("connection")
	it := query.Run(ctx)
	for {
		var conn struct{ Value string }
		_, err := it.Next(&conn)
		if err == datastore.Done {
			break
		} else if err != nil {
			return err
		}
		err = channel.SendJSON(ctx, conn.Value, message)
		if err != nil {
			return err
		}
	}
	return nil
}
func (api *API) handlePostMessage(res http.ResponseWriter, req *http.Request) error {
	ctx := appengine.NewContext(req)
	// handle incoming messages, send them out to everyone
	type Post struct {
		Text string
	}
	var post Post
	//	post := struct{
	//	Text string
	//	}{}
	err := json.NewDecoder(req.Body).Decode(&post)
	if err != nil {
		return err
	}
	log.Debugf(ctx, "the post is %s", post.Text)

	query := datastore.NewQuery("connections")
	it := query.Run(ctx)
	for {
		var connection struct{ Email string }
		_, err := it.Next(&connection)
		if err == datastore.Done {
			break
		} else if err != nil {
			return err
		}
		log.Debugf(ctx, "here is the email %s", connection.Email)
		err = channel.SendJSON(ctx, connection.Email, post)
		if err != nil {
			return err
		}
	}
	return nil
}
Beispiel #3
0
func (api *API) handlePostMessage(res http.ResponseWriter, req *http.Request) error {
	ctx := appengine.NewContext(req)
	type Message struct {
		Text string
	}
	var message Message
	err := json.NewDecoder(req.Body).Decode(&message)
	if err != nil {
		return err
	}
	fmt.Println("***********IS THE MESSAGE HERE: ", message)
	// u.Email == "*****@*****.**"
	room, err := getRoom(ctx)
	if err != nil {
		return err
	}
	for _, email := range room.Emails {
		err = channel.SendJSON(ctx, email, message)
		if err != nil {
			return err
		}
	}
	json.NewEncoder(res).Encode(true)
	return nil
}
Beispiel #4
0
Datei: main.go Projekt: pubnub/go
func sendResponseToChannel(w http.ResponseWriter, message string, r *http.Request, uuid string) {
	//c := context.NewContext(r)
	c := createContext(r)
	err := channel.SendJSON(c, uuid, message)
	log.Infof(c, "json")
	if err != nil {
		log.Errorf(c, "sending Game: %v", err)
	}
}
Beispiel #5
0
func handleSend(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	message := req.FormValue("message")
	err := channel.SendJSON(ctx, "example", message)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
	io.WriteString(res, "Sent!")
}
Beispiel #6
0
func pingHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	key := datastore.NewKey(ctx, ChannelInfoKind, ChannelInfoId, 0, nil)
	var channelInfo ChannelInfo
	if err := datastore.Get(ctx, key, &channelInfo); err != nil {
		failAndLog(ctx, w, http.StatusInternalServerError, "failed to get %s from datastore", key)
		return
	}

	log.Debugf(ctx, "fetched %s from datastore", key)

	payload := PushNotification{
		Msg: "hi!",
	}

	if err := channel.SendJSON(ctx, channelInfo.ClientId, &payload); err != nil {
		log.Errorf(ctx, "failed to send channel notification to %q: %s", channelInfo.ClientId, err)
	}
}