Documentation ¶
Overview ¶
Package xmpp provides GCM XMPP implementation.
Index ¶
- Constants
- Variables
- type Ack
- type Client
- func (c *Client) Close(ctx context.Context) error
- func (c *Client) Done() <-chan struct{}
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) SendData(ctx context.Context, msgID string, regID string, data interface{}, ...) (err error)
- func (c *Client) SendMessage(ctx context.Context, msgID string, regID string, notification Notification, ...) (err error)
- func (c *Client) State() int32
- type ClientOptions
- type Control
- type Endpoint
- type Handler
- type HandlerFunc
- type Nack
- type Notification
- type RealXMPPClientFactory
- type Receipt
- type SendOptions
- type XMPPClient
- type XMPPClientFactory
Examples ¶
Constants ¶
const ( // StateConnected which the connection establish and ready to be use. StateConnected int32 = iota // StateDraining which the connection is draining, should no any new message sent from here. StateDraining // StateClosing which the connection is in closing state. StateClosing // StateClosed which the connection already closed. StateClosed )
Variables ¶
var DefaultXMPPClientFactory = RealXMPPClientFactory{}
DefaultXMPPClientFactory is the default XMPPClientFactory.
Functions ¶
This section is empty.
Types ¶
type Ack ¶
type Ack struct { MessageID string // Original message id. From string // App registration token. CanonicalRegistrationID string // Canonical registration id. }
Ack message.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client of the GCM.
Example ¶
var ( senderID = 8832496 apiKey = "some-api-key" opts = xmpp.ClientOptions{} ) handler := xmpp.HandlerFunc(func(src, msg interface{}) error { switch v := msg.(type) { case xmpp.Ack: // TODO: handle ack fmt.Println("Ack for ", v.MessageID) return nil case xmpp.Nack: // TODO: handle nack fmt.Println("Nack for ", v.MessageID) return nil case xmpp.Receipt: // TODO: handle delivery receipt fmt.Println("Delivery receipt for ", v.MessageID, "status:", v.MessageStatus) return nil case xmpp.Control: // TODO: handle control message (ex: draining connection) return nil default: // TODO: unknown message return nil } }) client, err := xmpp.NewClient(senderID, apiKey, handler, opts) if err != nil { // TODO: handle error return } _ = client // TODO: client.Close() when client no longer used
Output:
func (*Client) Close ¶
Close the client.
Use the ctx cancelation/deadline wisely. If the cancelation initiateed or deadline exceed then the connection will be forced to close without waiting the un-ack responses.
func (*Client) Done ¶
func (c *Client) Done() <-chan struct{}
Done is done channel that will closed if all the resources released.
func (*Client) SendData ¶
func (c *Client) SendData(ctx context.Context, msgID string, regID string, data interface{}, opts SendOptions) (err error)
SendData to a destination app identified by regID.
Example ¶
var client *xmpp.Client // TODO: assign client ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() msgID := "some-unique-msg-id" to := "destination-token" opts := xmpp.SendOptions{} data := map[string]interface{}{ "text": "Hello World!", } if err := client.SendData(ctx, msgID, to, data, opts); err != nil { // TODO: handle error }
Output:
Example (Struct) ¶
type Notification struct { Text string `json:"text"` } var client *xmpp.Client // TODO: assign client ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() msgID := "some-unique-msg-id" to := "destination-token" opts := xmpp.SendOptions{} data := Notification{ Text: "Hello World!", } if err := client.SendData(ctx, msgID, to, data, opts); err != nil { // TODO: handle error }
Output:
func (*Client) SendMessage ¶
func (c *Client) SendMessage(ctx context.Context, msgID string, regID string, notification Notification, data interface{}, opts SendOptions) (err error)
SendMessage to a destination app identified by regID using FCM Message object
type ClientOptions ¶
type ClientOptions struct { Endpoint Endpoint // Used endpoint Default to Prod. MaxPendingMessages uint // Max pending messages. Default to 100. Debug bool // Enable debug mode. Default to false. XMPPClientFactory XMPPClientFactory // The XMPPClientFactory. Default to RealXMPPClientFactory. }
ClientOptions is the options for the client.
type Control ¶
type Control struct {
Type string // Control type, currently only CONNECTION_DRAINING.
}
Control message.
type Handler ¶
type Handler interface {
Handle(src, msg interface{}) error
}
Handler handle incoming message. Message can be Ack, Nack, Receipt, Control.
Message handling should not block too long. Long running message handling should be done in another go routine.
All returned error ignored except Receipt. Nil error will send back ack to the server, otherwise no ack (or nack) will be sent.
type HandlerFunc ¶
type HandlerFunc func(src, msg interface{}) error
HandlerFunc the the function adapter for Handler.
func (HandlerFunc) Handle ¶
func (f HandlerFunc) Handle(src, msg interface{}) error
Handle invoke f(msg).
type Nack ¶
type Nack struct { MessageID string // Original message id. From string // App registration id. Error string // Error code (ex: BAD_REGISTRATION, DEVICE_MESSAGE_RATE_EXCEEDED, INVALID_JSON). ErrorDescription string // Error description. }
Nack message.
type Notification ¶
type RealXMPPClientFactory ¶
type RealXMPPClientFactory struct {
Debug bool
}
RealXMPPClientFactory is the factory for real implementation of xmpp.Client
func (RealXMPPClientFactory) NewXMPPClient ¶
func (f RealXMPPClientFactory) NewXMPPClient(host, user, passwd string) (XMPPClient, error)
NewXMPPClient will create new XMPPClient.
type Receipt ¶
type Receipt struct { MessageStatus string // Message status (ex: MESSAGE_SENT_TO_DEVICE). MessageID string // Original message id. From string // App registration id. SentTime time.Time // Message sent timestamp. }
Receipt message.
type SendOptions ¶
type SendOptions struct { DryRun bool // Test without actually sending. Default to false. RequestDeliveryReceipt bool // Request for deliverey receipt. Default to false. TimeToLive time.Duration // Time to live. Default to 4 weeks, max up to 4 weeks. }
SendOptions is the send options.
type XMPPClient ¶
type XMPPClient interface { SendOrg(org string) (n int, err error) PingC2S(jid, server string) error Recv() (stanza interface{}, err error) Close() error }
XMPPClient is the XMPP Client. Extracted interface from xmpp.Client.
type XMPPClientFactory ¶
type XMPPClientFactory interface {
NewXMPPClient(host, user, passwd string) (XMPPClient, error)
}
XMPPClientFactory creates XMPPClient.