-
Notifications
You must be signed in to change notification settings - Fork 2
/
realtime.go
201 lines (159 loc) · 5.49 KB
/
realtime.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bytes"
"context"
"fmt"
"log"
"github.com/ably-labs/Ableye/config"
"github.com/ably-labs/Ableye/text"
ably "github.com/ably/ably-go/ably"
)
// createRealtimeClient creates a new realtime client and stores it in a connection.
// A clientID is also set on the client.
func createRealtimeClient(id connectionID) error {
var newClient *ably.Realtime
options := []ably.ClientOption{
ably.WithKey(config.Cfg.Key),
ably.WithClientID(id.string()),
}
if config.Cfg.DebugMode {
options = append(options, ably.WithLogLevel(ably.LogDebug))
}
client, err := ably.NewRealtime(options...)
if err != nil {
return err
}
newClient = client
connection := newRealtimeConnection(newClient, realtime)
connections[id] = &connection
log.Println(createRealtimeClientSuccess)
return nil
}
// closeRealtimeClient closes an existing realtime client and removes the connection.
func closeRealtimeClient(id connectionID) {
if connections[id] != nil && connections[id].realtimeClient != nil {
connections[id].realtimeClient.Close()
//Tear down the connection in internal memory.
connections[id].unsubscribe = nil
connections[id] = nil
log.Println(closeRealtimeClientSuccess)
}
}
// realtimeSetChannel sets the channel to the name provided in the channel name input text box.
func realtimeSetChannel(name string, id connectionID) {
newChannel := connections[id].realtimeClient.Channels.Get(name)
connections[id].realtimeChannel = newChannel
log.Println(setRealtimeChannelSuccess)
}
// detachChannel attaches a client to a channel.
func detachChannel(id connectionID) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
if err := connections[id].realtimeChannel.Detach(ctx); err != nil {
return err
}
log.Println(detachChannelSuccess)
return nil
}
// attachChannel attaches a client to a channel.
func attachChannel(id connectionID) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
if err := connections[id].realtimeChannel.Attach(ctx); err != nil {
return err
}
log.Println(attachChannelSuccess)
return nil
}
// realtimeSubscribeAll subscribes the connection's channel to all messsages.
// once subscribe events are output to the eventInfo text box
func realtimeSubscribeAll(id connectionID, eventInfo *text.Text) (func(), error) {
handlerFunc := newEventHandler(eventInfo)
unsubscribe, err := connections[id].realtimeChannel.SubscribeAll(connections[id].context, handlerFunc)
if err != nil {
return nil, err
}
log.Println(subscribeAllSuccess)
return unsubscribe, nil
}
//newEventHandler returns a function that can handle a message event.
//This pattern allows dependencies to be injected into the handler function.
func newEventHandler(eventInfo *text.Text) func(*ably.Message) {
return func(msg *ably.Message) {
log.Printf("Received message: name=%s data=%v\n", msg.Name, msg.Data)
if eventInfo != nil {
eventInfo.SetText(fmt.Sprintf("Event Received From : %s\n\n%s : %s %s: %s", msg.ClientID, messageNameText, msg.Name, messageDataText, msg.Data))
}
}
}
// unsubscribe calls a connections unsubscribe function if it exists.
func unsubscribe(id connectionID) {
if connections[id].unsubscribe != nil {
unsubscribeFunc := *connections[id].unsubscribe
unsubscribeFunc()
log.Println(unsubscribeSuccess)
}
}
// publishToRealtimeChannel publishes message name and message data to a realtime channel.
func publishToRealtimeChannel(id connectionID, messageName string, messageData interface{}) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
if err := connections[id].realtimeChannel.Publish(ctx, messageName, messageData); err != nil {
return err
}
log.Println(publishSuccess)
return nil
}
// enterPresence informs the channel that the client has entered the channel.
func enterPresence(id connectionID) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
if err := connections[id].realtimeChannel.Presence.Enter(ctx, nil); err != nil {
log.Println(err)
return err
}
log.Println(enterPresenceSuccess)
return nil
}
// getRealtimePresence sets the presence info text box to presence information.
func getRealtimePresence(id connectionID, presenceInfo *text.Text) {
var buffer bytes.Buffer
log.Println(startGetPresence)
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
presenceMessages, err := connections[id].realtimeChannel.Presence.Get(ctx)
if err != nil {
log.Println(err)
return
}
for i, msg := range presenceMessages {
if msg != nil {
buffer.WriteString(msg.ClientID)
// if not the last message, add a comma and a space.
if i != len(presenceMessages)-1 {
buffer.WriteString(", ")
}
}
}
presence := buffer.String()
presenceInfo.SetText(fmt.Sprintf("Presence : %s", presence))
log.Println(completeGetPresence)
}
// leavePresence removes the presence of a client from a channel.
func leavePresence(id connectionID) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
err := connections[id].realtimeChannel.Presence.Leave(ctx, nil)
if err != nil {
log.Println(err)
return err
}
log.Println(leavePresenceSuccess)
return nil
}