forked from bandabh/phxgoclient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
phxgoclient.go
182 lines (137 loc) · 3.43 KB
/
phxgoclient.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
package phxgoclient
import (
"errors"
"net/url"
"os"
"os/signal"
"time"
)
type PhxGoSocketStatus string
const (
PhxGoOpen PhxGoSocketStatus = "open"
PhxGoError PhxGoSocketStatus = "error"
PhxGoClosed PhxGoSocketStatus = "closed"
)
type PheonixGoSocket struct {
Host string
Schema string
Path string
RawQuery string
Status PhxGoSocketStatus
Timeout time.Duration
CustomAbsoultePath bool
Transport Transport
HeartbeatWorker *Worker
Channels map[string]*Channel
}
// Set your timeout intervel and heartbeat interval in format (interval * Duration) e.g (30 * time.Seconds)
func (phx *PheonixGoSocket) SetCustomTimeout(interval time.Duration) {
phx.Timeout = interval
return
}
func (phx *PheonixGoSocket) ClosePheonixWebsocket() {
if phx.HeartbeatWorker != nil {
phx.HeartbeatWorker.Shutdown()
}
phx.Status = PhxGoClosed
phx.Channels = nil
}
// Creates New Pheonix Websocket connection
func NewPheonixWebsocket(Host string, Path string, Schema string, CustomAbsoultePath bool, RawQuery string) PheonixGoSocket {
return PheonixGoSocket{
Host,
Schema,
Path,
RawQuery,
PhxGoClosed,
30 * time.Second,
CustomAbsoultePath,
PhxSocket,
nil,
make(map[string]*Channel),
}
}
func (client *Client) heartbeat() {
message := Message{
"phoenix", "heartbeat", nil, 0,
}
client.Socket.WriteJSON(message)
}
// Starts the Pheonix Websocket
func (phx PheonixGoSocket) Listen() error {
phx.Status = PhxGoOpen
path := phx.Path
if !phx.CustomAbsoultePath {
path = path + phx.Transport.ToPath()
}
u := url.URL{Scheme: phx.Schema, Host: phx.Host, Path: path, RawQuery: phx.RawQuery}
_, err := Connect(u)
if err != nil {
return err
}
// phx.HeartbeatWorker = NewWorker(phx.Timeout, func() {
// client.heartbeat()
// })
// go phx.HeartbeatWorker.Run()
return nil
}
// Raw access for a channel
func (phx *PheonixGoSocket) GetChannel(topic string) (*Channel, error) {
channel, ok := phx.Channels[topic]
if ok {
return channel, nil
} else {
return nil, errors.New("cannot get channel that does not exist")
}
}
func (phx *PheonixGoSocket) JoinChannel(topic string, payload interface{}) error {
channel, ok := phx.Channels[topic]
if ok {
channel := channel.Join(payload)
channel.Read()
phx.Channels[topic] = channel
return nil
} else {
return errors.New("channel does not exist, failed to join")
}
}
func (phx *PheonixGoSocket) OpenChannel(topic string) (*Channel, error) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
path := phx.Path
if !phx.CustomAbsoultePath {
path = path + phx.Transport.ToPath()
}
u := url.URL{Scheme: phx.Schema, Host: phx.Host, Path: path, RawQuery: phx.RawQuery}
client, err := Connect(u)
if err != nil {
phx.Status = PhxGoError
return nil, errors.New("dial:" + err.Error())
}
ch := client.MakeChannel(topic)
phx.HeartbeatWorker = NewWorker(phx.Timeout, func() {
ch.heartbeat()
})
go phx.HeartbeatWorker.Run()
cha, ok := phx.Channels[topic]
if !ok {
phx.Channels[topic] = &ch
} else {
return cha, nil
}
return &ch, nil
}
func (phx *PheonixGoSocket) CloseChannel(topic string) error {
channel, ok := phx.Channels[topic]
if ok {
channel.Leave()
if !channel.IsOpen() {
delete(phx.Channels, topic)
} else {
delete(phx.Channels, topic)
return errors.New("force closing channel, failed to gracefully leave")
}
return nil
}
return errors.New("channel does not exist or is already closed")
}