This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcm.go
135 lines (115 loc) · 2.82 KB
/
gcm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gcm
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"time"
"github.com/agl/xmpp-client/xmpp"
)
const (
ProductionAddr = "gcm.googleapis.com:5235"
TestingAddr = "gcm-preprod.googleapis.com:5236"
)
type Conn struct {
c *xmpp.Conn
rawc net.Conn
err error
}
func Dial(addr, senderID, apiKey string) (*Conn, error) {
con, err := tls.Dial("tcp", addr, nil)
if err != nil {
return nil, err
}
conf := &xmpp.Config{Conn: con, SkipTLS: true}
xcon, err := xmpp.Dial(addr, senderID, "gcm.googleapis.com", "", apiKey, conf)
if err != nil {
return nil, err
}
xcon.SetCustomStorage(xmpp.NsClient, "message", message{})
return &Conn{c: xcon, rawc: con}, nil
}
func (c *Conn) Send(m Message) error {
jsm, err := json.Marshal(m)
if err != nil {
return err
}
return c.c.SendStanza(wrapMsg(jsm))
}
func (c *Conn) Responses() <-chan Response {
rch := make(chan Response)
go func() {
defer close(rch)
for {
s, err := c.c.Next()
if err != nil {
c.err = err
return
}
m := s.Value.(*message)
if m.Error.Code != "" || m.Error.Text.Body != "" {
c.err = m.Error
return
}
resp := Response{}
if err = json.Unmarshal(m.GCM.Value, &resp); err != nil {
c.err = err
return
}
rch <- resp
}
}()
return rch
}
func (c *Conn) SetWriteTimeout(d time.Duration) error {
return c.rawc.SetWriteDeadline(time.Now().Add(d))
}
func (c *Conn) Close() error {
return c.rawc.Close()
}
func (c *Conn) Err() error {
return c.err
}
type Message struct {
To string `json:"to"`
ID string `json:"message_id"`
Type string `json:"message_type,omitempty"`
CollapseKey string `json:"collapse_key,omitempty"`
Data interface{} `json:"data"`
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
TTL int `json:"time_to_live,omitempty"`
DeliveryReceiptRequested bool `json:"delivery_receipt_requeste,omitempty"`
}
type Response struct {
Type string `json:"message_type"`
ID string `json:"message_id"`
From string `json:"from"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
type message struct {
ID string `xml:"id,attr"`
GCM gcmWrapper
Error gcmError `xml:"error,omitempty"`
}
type gcmError struct {
Code string `xml:"code,attr"`
Text gcmErrorText `xml:"text,omitempty"`
}
func (err gcmError) Error() string {
return fmt.Sprintf("%s: %s", err.Code, err.Text.Body)
}
type gcmErrorText struct {
Body string `xml:",innerxml"`
}
type gcmWrapper struct {
XMLName struct{} `xml:"google:mobile:data gcm"`
Value []byte `xml:",innerxml"`
}
func wrapMsg(m []byte) interface{} {
return message{
GCM: gcmWrapper{
Value: m,
},
}
}