@@ -6,43 +6,44 @@ import (
6
6
"time"
7
7
)
8
8
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
+
9
33
func Test_Client (t * testing.T ) {
10
34
t .Log ("Test Basic Client" )
11
35
12
- client , err := CreateClient (& ClientSettings {
36
+ client , err := CreateClient (ClientSettings {
13
37
Address : "localhost:8021" ,
14
38
Password : "fongopass" ,
15
- Timeout : 10 * time .Second ,
39
+ Timeout : 1 * time .Second ,
40
+ Listener : new (Nooper ),
16
41
})
17
42
18
43
if err != nil {
19
44
fmt .Println ("Something went wrong " , err )
20
45
}
21
46
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 ()
47
48
48
49
}
0 commit comments