Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.61 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "os"
  7.     "time"
  8.    
  9.     "flag"
  10.  
  11.     "github.com/sideshow/apns2"
  12.     "github.com/sideshow/apns2/certificate"
  13.     "github.com/sideshow/apns2/payload"
  14. )
  15.  
  16. func main() {
  17.     start := time.Now()
  18.     certPath := flag.String("cert", "", "Path to .pem certificate file (Required)")
  19.     count := flag.Int("count", 200, "Number of pushes to send")
  20.     token := flag.String("token", "", "Push token (Required)")
  21.     topic := flag.String("topic", "", "Topic (Required)")
  22.     flag.Parse()
  23.  
  24.     if *certPath == "" || *token == "" || *topic == "" {
  25.         flag.PrintDefaults()
  26.         os.Exit(1)
  27.     }
  28.  
  29.     cert, err := certificate.FromPemFile(*certPath, "")
  30.     if err != nil {
  31.         log.Fatal("Cert Error:", err)
  32.     }
  33.  
  34.     notifications := make(chan *apns2.Notification, 100)
  35.     responses := make(chan *apns2.Response, *count)
  36.  
  37.     client := apns2.NewClient(cert).Production()
  38.  
  39.     for i := 0; i < 5000; i++ {
  40.         go worker(client, notifications, responses)
  41.     }
  42.  
  43.     for i := 0; i < *count; i++ {
  44.         n := &apns2.Notification{
  45.             DeviceToken: *token,
  46.             Topic:       *topic,
  47.             Payload:     payload.NewPayload().Alert(""),
  48.         }
  49.         notifications <- n
  50.     }
  51.  
  52.     for i := 0; i < *count; i++ {
  53.         res := <-responses
  54.         fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
  55.     }
  56.  
  57.     close(notifications)
  58.     close(responses)
  59.  
  60.     elapsed := time.Since(start)
  61.     log.Printf("Sent took %s", elapsed)
  62. }
  63.  
  64. func worker(client *apns2.Client, notifications <-chan *apns2.Notification, responses chan<- *apns2.Response) {
  65.     for n := range notifications {
  66.         res, err := client.Push(n)
  67.         if err != nil {
  68.             log.Fatal("Push Error:", err)
  69.         }
  70.         responses <- res
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement