-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.go
87 lines (75 loc) · 1.64 KB
/
connection.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
package main
import (
"context"
ably "github.com/ably/ably-go/ably"
)
func init() {
// Initialise a map to store connections to the ably platform.
connections = make(map[connectionID]*connection)
}
var (
connections map[connectionID]*connection
)
type connectionID int
func (c connectionID) string() string {
switch int(c) {
case 0:
return "Client A"
case 1:
return "Client B"
case 2:
return "Client C"
case 3:
return "Client D"
}
return ""
}
const (
clientA connectionID = iota
clientB
clientC
clientD
)
type connectionType int
const (
realtime connectionType = iota
rest
)
func (c connectionType) string() string {
switch int(c) {
case 0:
return "Realtime Client"
case 1:
return "Rest Client"
}
return ""
}
// connection represents a connection to the Ably platform.
type connection struct {
context context.Context
connectionType *connectionType
restClient *ably.REST
realtimeClient *ably.Realtime
realtimeChannel *ably.RealtimeChannel
restChannel *ably.RESTChannel
unsubscribe *func()
channelDetails *ably.ChannelDetails
}
// newRealtimeConnection is a contructor to create a new realtime connection.
func newRealtimeConnection(client *ably.Realtime, conType connectionType) connection {
ctx := context.Background()
return connection{
context: ctx,
connectionType: &conType,
realtimeClient: client,
}
}
// newRestConnection is a contructor to create a new REST connection.
func newRestConnection(client *ably.REST, conType connectionType) connection {
ctx := context.Background()
return connection{
context: ctx,
connectionType: &conType,
restClient: client,
}
}