Giter Club home page Giter Club logo

go_aliyun_mns's Introduction

阿里云 MNS 的 Golang 封装

拉过来

go get -u github.com/suifengtec/go_aliyun_mns/mns

使用

package main

import (
	"encoding/xml"
	"fmt"
	"github.com/suifengtec/go_aliyun_mns/mns"
)

var (
	AliYunAccessKeyId     = "LT***bw"
	AliYunAccessKeySecret = "gF3***z5tAe"
	AliYunMnsEndpoint     = "1***37.mns.cn-shanghai.aliyuncs.com"
	AliYunMnsQueueName    = "n***ForNewOrder"
)

func send() {

	client := mns.NewClient(AliYunAccessKeyId, AliYunAccessKeySecret, AliYunMnsEndpoint)

	queue := mns.Queue{
		Client:    client,
		QueueName: AliYunMnsQueueName,
		Base64:    false,
	}

	msg := mns.Message{MessageBody: "世界,你好"}
	data, err := xml.Marshal(msg)
	if err != nil {
		fmt.Println(err)
	}

	msgId, err2 := queue.Send(mns.GetCurrentUnixMicro(), data)
	if err2 != nil {
		fmt.Println(err2)
	} else {
		fmt.Printf("send message=>%v", msgId)
		fmt.Println()
	}

}

func receive() {

	client := mns.NewClient(AliYunAccessKeyId, AliYunAccessKeySecret, AliYunMnsEndpoint)
	queue := mns.Queue{
		Client:    client,
		QueueName: AliYunMnsQueueName,
		Base64:    false,
	}
	respChan := make(chan mns.MsgReceive)
	errChan := make(chan error)
	end := make(chan int)
	receiptHandle := ""

	go func() {
		select {
		case resp := <-respChan:
			{
				fmt.Printf("receive message=>%v", resp)
				receiptHandle = resp.ReceiptHandle
				end <- 1
			}
		case err := <-errChan:
			{
				fmt.Println(err)
				end <- 0
			}
		}
	}()

	queue.Receive(respChan, errChan)
	received := <-end

	if received == 1 {
		msgDelete(receiptHandle)
	}

}

func msgDelete(receiptHandle string) {

	client := mns.NewClient(AliYunAccessKeyId, AliYunAccessKeySecret, AliYunMnsEndpoint)
	queue := mns.Queue{
		Client:    client,
		QueueName: AliYunMnsQueueName,
		Base64:    false,
	}
	errChan := make(chan error)
	end := make(chan int)
	go func() {
		select {
		case err := <-errChan:
			{
				if err != nil {

					fmt.Println(err)

					end <- 0
				} else {

					fmt.Println()

					fmt.Println("deletesuccess=>" + receiptHandle)
					end <- 1
				}
			}
		}
	}()

	queue.Delete(receiptHandle, errChan)
	<-end
}

func main() {

	fmt.Println("Aliyun MNS")

	//send()
	receive()

}

go_aliyun_mns's People

Contributors

suifengtec avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.