func (self *ApnsHttpServer) decodePayload(req *http.Request, resp *response) (string, *entry.PayLoad) { tokenV := req.PostFormValue("token") sound := req.PostFormValue("sound") badgeV := req.PostFormValue("badge") body := req.PostFormValue("body") //-----------检查参数 valid := checkArguments(tokenV, sound, badgeV, body) if !valid { resp.Status = RESP_STATUS_PUSH_ARGUMENTS_INVALID resp.Error = errors.New("Notification Params are Invalid!") return "", nil } tokenSplit := regx.FindAllString(tokenV, -1) var token string = "" for _, v := range tokenSplit { token += v } badge, _ := strconv.ParseInt(badgeV, 10, 32) //拼接payload payload := entry.NewSimplePayLoad(sound, int(badge), body) //是个大的Json数据即可 extArgs := req.PostFormValue("extArgs") if len(extArgs) > 0 { var jsonMap map[string]interface{} err := json.Unmarshal([]byte(extArgs), &jsonMap) if nil != err { resp.Status = RESP_STATUS_PAYLOAD_BODY_DECODE_ERROR resp.Error = errors.New("PAYLOAD BODY DECODE ERROR!") } else { for k, v := range jsonMap { //如果存在数据嵌套则返回错误,不允许数据多层嵌套 if reflect.TypeOf(v).Kind() == reflect.Map { resp.Status = RESP_STATUS_PAYLOAD_BODY_DEEP_ITERATOR resp.Error = errors.New("DEEP PAYLOAD BODY ITERATOR!") break } else { payload.AddExtParam(k, v) } } } } return token, payload }
func TestPoolSendMessage(t *testing.T) { cert, err := tls.LoadX509KeyPair(CERT_PATH, KEY_PATH) if nil != err { t.Logf("READ CERT FAIL|%s", err.Error()) t.Fail() return } responseChan := make(chan *entry.Response, 10) feedbackChan := make(chan *entry.Feedback, 1000) body := "hello apns" payload, _ := entry.NewSimplePayLoad("ms.caf", 1, body) client := NewDefaultApnsClient(cert, PUSH_APPLE, feedbackChan, FEED_BACK_APPLE, entry.NewCycleLink(3, 100)) for i := 0; i < 1; i++ { err := client.SendEnhancedNotification(1, math.MaxUint32, apnsToken, *payload) // err := client.SendSimpleNotification(apnsToken, payload) t.Logf("SEND NOTIFY|%s\n", err) } go func() { //测试feedback err := client.FetchFeedback(50) if nil != err { t.Logf("FETCH FEEDBACK|FAIL |%s\n", err) } }() for i := 0; i < 2; i++ { select { case <-time.After(20 * time.Second): case resp := <-responseChan: t.Logf("===============%t|EXIT", resp) //如果有返回错误则说明发送失败的 t.Fail() case fb := <-feedbackChan: i := 0 for i < 100 { t.Logf("FEEDBACK===============%s|EXIT", fb) i++ } //如果有返回错误则说明发送失败的 } } client.Destory() // }
func main() { feedbackChan := make(chan *entry.Feedback, 1000) cert, _ := tls.LoadX509KeyPair(CERT_PATH, KEY_PATH) client := apns.NewMockApnsClient(cert, PUSH_APPLE, feedbackChan, FEED_BACK_APPLE, entry.NewCycleLink(3, 100000)) payload := entry.NewSimplePayLoad("ms.caf", int(1), "hello") for i := 0; i < 100; i++ { go func(a int) { for j := 0; j < 1000000; j++ { client.SendEnhancedNotification(1, 1, apnsToken, *payload) } fmt.Printf("finish:%d\n", a) }(i) } select {} }