forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.go
84 lines (76 loc) · 1.74 KB
/
response.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
package messaging
import (
"fmt"
)
type responseType int
type errorType int
const (
channelResponse responseType = 1 << iota
channelGroupResponse
wildcardResponse
)
type successResponse struct {
Data []byte
Channel string
Source string
Timetoken string
Presence bool
Type responseType
}
func (r successResponse) Bytes() []byte {
switch r.Type {
case wildcardResponse:
fallthrough
case channelGroupResponse:
return []byte(fmt.Sprintf(
"[[%s], \"%s\", \"%s\", \"%s\"]", r.Data, r.Timetoken,
removePnpres(r.Channel), removePnpres(r.Source)))
case channelResponse:
fallthrough
default:
return []byte(fmt.Sprintf(
"[[%s], \"%s\", \"%s\"]", r.Data, r.Timetoken, removePnpres(r.Channel)))
}
}
type errorResponse struct {
Message string
DetailedMessage string
Reason responseStatus
Type responseType
}
func (e errorResponse) StringForSource(source string) string {
// TODO: handle all reasons
switch e.Reason {
case responseAlreadySubscribed:
fallthrough
case responseNotSubscribed:
if e.DetailedMessage != "" {
return fmt.Sprintf("[0, \"%s %s '%s' %s\", %s, \"%s\"]",
stringPresenceOrSubscribe(source),
e.Type,
source,
e.Reason,
e.DetailedMessage,
source)
}
return fmt.Sprintf("[0, \"%s %s '%s' %s\", \"%s\"]",
stringPresenceOrSubscribe(source),
e.Type,
source,
e.Reason,
source)
case responseTimedOut:
return fmt.Sprintf("[0, \"%s %s %s\", \"%s\"]",
stringPresenceOrSubscribe(source),
e.Type,
e.Reason,
source)
case responseAsIsError:
fallthrough
default:
return fmt.Sprintf("[0, \"%s\", \"%s\"]", e.Message, source)
}
}
func (e errorResponse) BytesForSource(source string) []byte {
return []byte(e.StringForSource(source))
}