4
4
"encoding/hex"
5
5
"fmt"
6
6
"github.com/codedust/go-tox"
7
- "os"
8
- "os/signal"
9
7
"time"
10
8
)
11
9
@@ -15,6 +13,8 @@ type Server struct {
15
13
PublicKey []byte
16
14
}
17
15
16
+ var counter int = 0
17
+
18
18
func main () {
19
19
alice , err := gotox .New (nil )
20
20
if err != nil {
@@ -29,10 +29,10 @@ func main() {
29
29
bob .SelfSetName ("BobBot" )
30
30
31
31
aliceAddr , _ := alice .SelfGetAddress ()
32
- fmt .Println ("ID alice: " , hex .EncodeToString (aliceAddr ))
32
+ fmt .Println ("[ ID alice] " , hex .EncodeToString (aliceAddr ))
33
33
34
34
bobAddr , _ := bob .SelfGetAddress ()
35
- fmt .Println ("ID bob: " , hex .EncodeToString (bobAddr ))
35
+ fmt .Println ("[ ID bob] " , hex .EncodeToString (bobAddr ))
36
36
37
37
// We can set the same callback function for both *Tox instances
38
38
bob .CallbackFriendRequest (onFriendRequest )
@@ -49,8 +49,8 @@ func main() {
49
49
* Use more than one node in a real world szenario. This example relies one
50
50
* the following node to be up.
51
51
*/
52
- pubkey , _ := hex .DecodeString ("04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F " )
53
- server := & Server {"144.76.60.215 " , 33445 , pubkey }
52
+ pubkey , _ := hex .DecodeString ("B75583B6D967DB8AD7C6D3B6F9318194BCC79B2FEF18F69E2DF275B779E7AA30 " )
53
+ server := & Server {"maggie.prok.pw " , 33445 , pubkey }
54
54
55
55
err = alice .Bootstrap (server .Address , server .Port , server .PublicKey )
56
56
if err != nil {
@@ -63,39 +63,40 @@ func main() {
63
63
64
64
isRunning := true
65
65
66
- c := make (chan os.Signal , 1 )
67
- signal .Notify (c , os .Interrupt )
68
66
ticker := time .NewTicker (25 * time .Millisecond )
69
67
70
- times := 0
71
68
for isRunning {
72
69
select {
73
- case <- c :
74
- // Press ^C to trigger those events
75
- if times == 0 {
70
+ case <- ticker . C :
71
+ if counter == 2 {
72
+ time . Sleep ( 2 * time . Second )
76
73
// First Bob adds Alice
77
74
bob .FriendAdd (aliceAddr , "Hey Alice, wanna be my friend. ;)" )
78
- fmt .Printf ("[BobBot] Friend request send. Waiting for Alice to response.\n " )
79
- } else if times == 1 {
75
+ fmt .Printf ("[BobBot] Friend request send. Waiting for Alice to respond.\n " )
76
+ counter ++
77
+ } else if counter == 6 {
78
+ time .Sleep (2 * time .Second )
80
79
// Then Bob sends a message to Alice
81
80
friendnumbers , _ := bob .SelfGetFriendlist ()
82
81
_ , err := bob .FriendSendMessage (friendnumbers [0 ], gotox .TOX_MESSAGE_TYPE_NORMAL , "HELLO ALICE" )
83
82
fmt .Printf ("[BobBot] Sending message to Alice (friendnumber: %d, error: %v)\n " , friendnumbers [0 ], err )
84
- } else if times == 2 {
83
+ counter ++
84
+ } else if counter == 8 {
85
+ time .Sleep (2 * time .Second )
85
86
// Alice responds to Bob
86
87
friendnumbers , _ := alice .SelfGetFriendlist ()
87
88
_ , err := alice .FriendSendMessage (friendnumbers [0 ], gotox .TOX_MESSAGE_TYPE_NORMAL , "Hey Bob!" )
88
89
fmt .Printf ("[AliceBot] Sending message to Bob (friendnumber: %d, error: %v)\n " , friendnumbers [0 ], err )
89
- } else {
90
+ counter ++
91
+ } else if counter == 10 {
92
+ time .Sleep (2 * time .Second )
90
93
// We then put an end to their love
91
- fmt .Println ("Killing" )
94
+ fmt .Println ("\\ o/ It worked! Killing... " )
92
95
isRunning = false
93
96
alice .Kill ()
94
97
bob .Kill ()
98
+ break
95
99
}
96
- times += 1
97
- break
98
- case <- ticker .C :
99
100
alice .Iterate ()
100
101
bob .Iterate ()
101
102
break
@@ -104,6 +105,7 @@ func main() {
104
105
}
105
106
106
107
func onFriendRequest (t * gotox.Tox , publicKey []byte , message string ) {
108
+ counter ++
107
109
name , _ := t .SelfGetName ()
108
110
fmt .Printf ("[%s] New friend request from %s\n " , name , hex .EncodeToString (publicKey ))
109
111
@@ -113,17 +115,20 @@ func onFriendRequest(t *gotox.Tox, publicKey []byte, message string) {
113
115
}
114
116
115
117
func onFriendMessage (t * gotox.Tox , friendnumber uint32 , messageType gotox.ToxMessageType , message string ) {
118
+ counter ++
116
119
name , _ := t .SelfGetName ()
117
120
friend , _ := t .FriendGetName (friendnumber )
118
121
fmt .Printf ("[%s] New message from %s : %s\n " , name , friend , message )
119
122
}
120
123
121
124
func onFriendConnectionStatusChanges (t * gotox.Tox , friendnumber uint32 , connectionstatus gotox.ToxConnection ) {
125
+ counter ++
122
126
name , _ := t .SelfGetName ()
123
127
fmt .Printf ("[%s] Connection status of friend changed to %v\n " , name , connectionstatus )
124
128
}
125
129
126
130
func onSelfConnectionStatusChanges (t * gotox.Tox , connectionstatus gotox.ToxConnection ) {
131
+ counter ++
127
132
name , _ := t .SelfGetName ()
128
133
fmt .Printf ("[%s] Connection status changed to %v\n " , name , connectionstatus )
129
134
}
0 commit comments