ref: 35e68c94e44a7825beaae29f14feb588b365c624 nim_tox/README.md -rw-r--r-- 1.1 KiB
35e68c94Emery Hemingway Fix typo at offflinePeerLastActive 9 months ago

Nim wrapper over the Toxcore library

import toxcore

from std/os import sleep

  bootstrapHost = "tox.neuland.technology"
  bootstrapKey = "15E9C309CFCB79FDDF0EBA057DABB49FE15F3803B1BFF06536AE2E5BA5E4690E".toPublicKey

proc setupCallbacks(bot: Tox) =
  var echoCount = 0
    # bind a value for callback closure magic

  bot.onFriendRequest do (pk: PublicKey; msg: string):
    discard bot.addFriendNoRequest(pk)

  bot.onFriendMessage do (f: Friend; msg: string; kind: TOX_MESSAGE_TYPE):
    discard bot.send(f, msg, kind)
    inc echoCount
    bot.statusMessage = $echoCount & " echos served "

proc newEchoBot(name: string): Tox =
  result = newTox()
  result.name = name
  result.bootstrap(bootstrapHost, bootstrapKey)
  echo result.name, " echos messages to ", result.address
  echo result.name, " joins DHT with port ", result.udpPort, " address ", result.dhtId

  a = newEchoBot "alice"
  b = newEchoBot "bob"
while true:
  iterate a
  iterate b
  sleep min(a.iterationInterval, b.iterationInterval)