-
Notifications
You must be signed in to change notification settings - Fork 6
/
message.go
137 lines (115 loc) · 2.85 KB
/
message.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
136
137
package signalr
import (
stdJSON "encoding/json"
)
// MessageType -
type MessageType int
const (
MessageTypeInvocation MessageType = iota + 1
MessageTypeStreamItem
MessageTypeCompletion
MessageTypeStreamInvocation
MessageTypeCancelInvocation
MessageTypePing
MessageTypeCloseMessage
)
// HandshakeRequest -
type HandshakeRequest struct {
Protocol string `json:"protocol"`
Version int `json:"version"`
}
func newHandshakeRequest() HandshakeRequest {
return HandshakeRequest{
Protocol: "json",
Version: 1,
}
}
// Error -
type Error struct {
Error string `json:"string,omitempty"`
}
// Type -
type Type struct {
Type MessageType `json:"type"`
}
// Message -
type Message struct {
Type
ID string `json:"invocationId"`
Headers map[string]string `json:"headers,omitempty"`
}
// Invocation - a `Invocation` message
type Invocation struct {
Message
Target string `json:"target"`
Arguments []stdJSON.RawMessage `json:"arguments"`
StreamsID []string `json:"streamIds,omitempty"`
}
// StreamInvocation - a `StreamInvocation` message
type StreamInvocation Invocation
// StreamItem - a `StreamItem` message
type StreamItem struct {
Message
Item string `json:"item"`
}
// Completion - a `Completion` message
type Completion struct {
Message
Result uint64 `json:"result,omitempty"`
Error string `json:"string,omitempty"`
}
// CancelInvocation - a `CancelInvocation` message
type CancelInvocation Message
// PingMessage - a `PingMessage` message
type PingMessage Type
// CloseMessage - a `CloseMessage` message
type CloseMessage struct {
Type
Error string `json:"string,omitempty"`
AllowReconnect bool `json:"allowReconnect,omitempty"`
}
func newCloseMessage() CloseMessage {
return CloseMessage{
Type: Type{
Type: MessageTypeCloseMessage,
},
}
}
// NegotiateResponse -
type NegotiateResponse struct {
ConnectionToken string `json:"connectionToken"`
ConnectionID string `json:"connectionId"`
NegotiateVersion int `json:"negotiateVersion"`
AvailableTransports []AvailableTransport `json:"availableTransports"`
}
// AvailableTransport -
type AvailableTransport struct {
Transport string `json:"transport"`
TransferFormats []string `json:"transferFormats"`
}
// RedirectResponse -
type RedirectResponse struct {
URL string `json:"url"`
AccessToken string `json:"accessToken"`
}
// NewInvocation -
func NewInvocation(id, target string, args ...interface{}) Invocation {
if args == nil {
args = make([]interface{}, 0)
}
arguments := make([]stdJSON.RawMessage, len(args))
for i := range args {
data, err := json.Marshal(args[i])
if err == nil {
arguments[i] = data
}
}
return Invocation{
Message: Message{
Type: Type{MessageTypeInvocation},
ID: id,
},
Target: target,
Arguments: arguments,
}
}