Esempio n. 1
0
func SignWithSecret(c common.Context, s string) string {
	key := appSecret(c)

	c.Debugf("Pusher signing %s with key %s", s, key)

	h := hmac.New(sha256.New, []byte(key))
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil))
}
Esempio n. 2
0
func triggerEvent(c common.Context, channel, event string, data interface{}, socketId string) error {
	if data == nil {
		data = map[string]string{}
	}

	dataBytes, err := json.Marshal(data)
	if err != nil {
		return err
	}

	e := pusherEvent{
		Channel:  channel,
		Name:     event,
		SocketId: socketId,
		Data:     string(dataBytes),
	}

	body, err := json.Marshal(e)
	if err != nil {
		return err
	}

	client := urlfetch.Client(c)
	eventsPath := appPath(c) + "/events"
	params := authParams(c, eventsPath, body)

	url := fmt.Sprintf("%s?%s", appUrl(c)+"/events", params)

	resp, err := client.Post(url, "application/json", bytes.NewBuffer(body))
	if err != nil {
		c.Errorf("Failed sending to pusher: %s", err)
		return err
	}

	defer resp.Body.Close()
	res, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	if resp.StatusCode != 200 {
		c.Errorf("Unexpected status code: %d with response: %s", resp.StatusCode, string(res))
	}

	return nil
}