Skip to content

Commit 8eee8b5

Browse files
committed
New interface. WAY CLEANER
1 parent 675ef84 commit 8eee8b5

8 files changed

+315
-474
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.test

client.go

-101
This file was deleted.

client_test.go

+28-27
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,44 @@ import (
66
"time"
77
)
88

9+
type Nooper struct{}
10+
11+
func (n *Nooper) OnConnect(con *Connection) {
12+
fmt.Println("Connected")
13+
evt, err := con.Send("event", "plain", "HEARTBEAT")
14+
if err != nil {
15+
fmt.Println("ahn?")
16+
return
17+
}
18+
fmt.Println(evt.Success)
19+
}
20+
21+
func (n *Nooper) OnDisconnect(con *Connection) {
22+
fmt.Println("disconnect")
23+
}
24+
25+
func (n *Nooper) OnEvent(con *Connection, evt *Event) {
26+
fmt.Println("event", evt.EventBody.Get("Event-Name"))
27+
}
28+
29+
func (n *Nooper) OnClose(con *Connection) {
30+
fmt.Println("close")
31+
}
32+
933
func Test_Client(t *testing.T) {
1034
t.Log("Test Basic Client")
1135

12-
client, err := CreateClient(&ClientSettings{
36+
client, err := CreateClient(ClientSettings{
1337
Address: "localhost:8021",
1438
Password: "fongopass",
15-
Timeout: 10 * time.Second,
39+
Timeout: 1 * time.Second,
40+
Listener: new(Nooper),
1641
})
1742

1843
if err != nil {
1944
fmt.Println("Something went wrong ", err)
2045
}
2146

22-
disconnecting := false
23-
for loop := true; loop; {
24-
msg := <-client.EventsChannel
25-
26-
//For clients, 'client' and 'msg.Connection' will point to the same struct
27-
//However, 'client' has a few more fields available
28-
switch {
29-
case msg.Type == EventState && !msg.Success:
30-
fmt.Println("Disconnected")
31-
loop = false
32-
case msg.Type == EventState && msg.Success:
33-
fmt.Println("Connected")
34-
resp, err := msg.Connection.Send("event", "json", "RELOADXML")
35-
fmt.Println(resp, err)
36-
resp, err = msg.Connection.Send("api", "reloadxml")
37-
38-
fmt.Println(resp, err)
39-
case msg.Type == EventGeneric:
40-
//get one event and disconnect
41-
if !disconnecting {
42-
msg.Connection.Send("exit")
43-
disconnecting = true
44-
}
45-
}
46-
}
47+
client.Loop()
4748

4849
}

commands.go

-117
This file was deleted.

0 commit comments

Comments
 (0)