Beispiel #1
0
func main() {
	var filepath string

	flag.StringVar(&filepath, "save", "", "path to save file")
	flag.Parse()

	server := &Server{"37.187.46.132", 33445, "A9D98212B3F972BD11DA52BEB0658C326FCCC1BFD49F347F9C2D3D8B61E1B927"}

	o := &golibtox.Options{true, false, false, "127.0.0.1", 5555}

	tox, err := golibtox.New(o)
	if err != nil {
		panic(err)
	}

	// If no data could be loaded, we should set the name
	if err := loadData(tox, filepath); err != nil {
		tox.SetName("GolibtoxBot")
	}

	tox.SetStatusMessage([]byte("golibtox is cool!"))

	addr, _ := tox.GetAddress()
	fmt.Println("ID: ", hex.EncodeToString(addr))

	err = tox.SetUserStatus(golibtox.USERSTATUS_NONE)

	// Register our callbacks
	tox.CallbackFriendRequest(onFriendRequest)
	tox.CallbackFriendMessage(onFriendMessage)
	tox.CallbackFileSendRequest(onFileSendRequest)
	tox.CallbackFileControl(onFileControl)
	tox.CallbackFileData(onFileData)

	// Connect to the network
	err = tox.BootstrapFromAddress(server.Address, server.Port, server.PublicKey)
	if err != nil {
		panic(err)
	}

	isRunning := true

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

	for isRunning {
		select {
		case <-c:
			fmt.Println("Saving...")
			if err := saveData(tox, filepath); err != nil {
				fmt.Println(err)
			}
			fmt.Println("Killing")
			isRunning = false
			tox.Kill()
		case <-ticker.C:
			tox.Do()
		}
	}
}
Beispiel #2
0
func main() {

	server := &Server{"37.187.46.132", 33445, "A9D98212B3F972BD11DA52BEB0658C326FCCC1BFD49F347F9C2D3D8B61E1B927"}

	alice, err := golibtox.New(nil)
	if err != nil {
		panic(err)
	}
	bob, err := golibtox.New(nil)
	if err != nil {
		panic(err)
	}

	alice.SetName("AliceBot")
	bob.SetName("BobBot")

	aliceAddr, _ := alice.GetAddress()
	fmt.Println("ID alice: ", hex.EncodeToString(aliceAddr))

	bobAddr, _ := bob.GetAddress()
	fmt.Println("ID bob: ", hex.EncodeToString(bobAddr))

	// We can set the same callback function for both *Tox instances
	alice.CallbackFriendRequest(onFriendRequest)
	bob.CallbackFriendRequest(onFriendRequest)

	alice.CallbackFriendMessage(onFriendMessage)
	bob.CallbackFriendMessage(onFriendMessage)

	err = alice.BootstrapFromAddress(server.Address, server.Port, server.PublicKey)
	if err != nil {
		panic(err)
	}
	err = bob.BootstrapFromAddress(server.Address, server.Port, server.PublicKey)
	if err != nil {
		panic(err)
	}

	isRunning := true

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

	times := 0
	for isRunning {
		select {
		case <-c:
			// Press ^C to trigger those events
			if times == 0 {
				// First Bob adds Alice
				bob.AddFriend(aliceAddr, []byte("o"))
			} else if times == 1 {
				// Then Bob sends a message to Alice
				bob.SendMessage(0, []byte("HELLO ALICE"))
			} else if times == 2 {
				// Alice responds to Bob
				alice.SendMessage(0, []byte("Hey Bob !"))
			} else {
				// We then put an end to their love
				fmt.Println("Killing")
				isRunning = false
				alice.Kill()
				bob.Kill()
			}
			times += 1
			break
		case <-ticker.C:
			alice.Do()
			bob.Do()
			break
		}
	}
}