Skip to content

Commit a63679e

Browse files
committed
Improve examples/dual.go
1 parent 242eda5 commit a63679e

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

examples/dual.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"encoding/hex"
55
"fmt"
66
"github.com/codedust/go-tox"
7-
"os"
8-
"os/signal"
97
"time"
108
)
119

@@ -15,6 +13,8 @@ type Server struct {
1513
PublicKey []byte
1614
}
1715

16+
var counter int = 0
17+
1818
func main() {
1919
alice, err := gotox.New(nil)
2020
if err != nil {
@@ -29,10 +29,10 @@ func main() {
2929
bob.SelfSetName("BobBot")
3030

3131
aliceAddr, _ := alice.SelfGetAddress()
32-
fmt.Println("ID alice: ", hex.EncodeToString(aliceAddr))
32+
fmt.Println("[ID alice]", hex.EncodeToString(aliceAddr))
3333

3434
bobAddr, _ := bob.SelfGetAddress()
35-
fmt.Println("ID bob: ", hex.EncodeToString(bobAddr))
35+
fmt.Println("[ID bob]", hex.EncodeToString(bobAddr))
3636

3737
// We can set the same callback function for both *Tox instances
3838
bob.CallbackFriendRequest(onFriendRequest)
@@ -49,8 +49,8 @@ func main() {
4949
* Use more than one node in a real world szenario. This example relies one
5050
* the following node to be up.
5151
*/
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}
5454

5555
err = alice.Bootstrap(server.Address, server.Port, server.PublicKey)
5656
if err != nil {
@@ -63,39 +63,40 @@ func main() {
6363

6464
isRunning := true
6565

66-
c := make(chan os.Signal, 1)
67-
signal.Notify(c, os.Interrupt)
6866
ticker := time.NewTicker(25 * time.Millisecond)
6967

70-
times := 0
7168
for isRunning {
7269
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)
7673
// First Bob adds Alice
7774
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)
8079
// Then Bob sends a message to Alice
8180
friendnumbers, _ := bob.SelfGetFriendlist()
8281
_, err := bob.FriendSendMessage(friendnumbers[0], gotox.TOX_MESSAGE_TYPE_NORMAL, "HELLO ALICE")
8382
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)
8586
// Alice responds to Bob
8687
friendnumbers, _ := alice.SelfGetFriendlist()
8788
_, err := alice.FriendSendMessage(friendnumbers[0], gotox.TOX_MESSAGE_TYPE_NORMAL, "Hey Bob!")
8889
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)
9093
// We then put an end to their love
91-
fmt.Println("Killing")
94+
fmt.Println("\\o/ It worked! Killing...")
9295
isRunning = false
9396
alice.Kill()
9497
bob.Kill()
98+
break
9599
}
96-
times += 1
97-
break
98-
case <-ticker.C:
99100
alice.Iterate()
100101
bob.Iterate()
101102
break
@@ -104,6 +105,7 @@ func main() {
104105
}
105106

106107
func onFriendRequest(t *gotox.Tox, publicKey []byte, message string) {
108+
counter++
107109
name, _ := t.SelfGetName()
108110
fmt.Printf("[%s] New friend request from %s\n", name, hex.EncodeToString(publicKey))
109111

@@ -113,17 +115,20 @@ func onFriendRequest(t *gotox.Tox, publicKey []byte, message string) {
113115
}
114116

115117
func onFriendMessage(t *gotox.Tox, friendnumber uint32, messageType gotox.ToxMessageType, message string) {
118+
counter++
116119
name, _ := t.SelfGetName()
117120
friend, _ := t.FriendGetName(friendnumber)
118121
fmt.Printf("[%s] New message from %s : %s\n", name, friend, message)
119122
}
120123

121124
func onFriendConnectionStatusChanges(t *gotox.Tox, friendnumber uint32, connectionstatus gotox.ToxConnection) {
125+
counter++
122126
name, _ := t.SelfGetName()
123127
fmt.Printf("[%s] Connection status of friend changed to %v\n", name, connectionstatus)
124128
}
125129

126130
func onSelfConnectionStatusChanges(t *gotox.Tox, connectionstatus gotox.ToxConnection) {
131+
counter++
127132
name, _ := t.SelfGetName()
128133
fmt.Printf("[%s] Connection status changed to %v\n", name, connectionstatus)
129134
}

0 commit comments

Comments
 (0)