nim-chat-poc/examples/pingpong.nim

91 lines
2.9 KiB
Nim
Raw Normal View History

2025-09-28 14:45:02 -07:00
import chronicles
import chronos
2025-10-09 18:46:36 -07:00
import strformat
2025-09-28 14:45:02 -07:00
import chat
2025-09-28 14:45:02 -07:00
import content_types
2025-10-09 18:46:36 -07:00
# TEsting
import ../src/chat/crypto
2025-10-09 18:46:36 -07:00
2025-09-28 14:45:02 -07:00
proc getContent(content: ContentFrame): string =
# Skip type checks and assume its a TextFrame
let m = decode(content.bytes, TextFrame).valueOr:
2025-10-09 18:46:36 -07:00
raise newException(ValueError, fmt"Badly formed Content")
return fmt"{m}"
2025-09-28 14:45:02 -07:00
proc main() {.async.} =
# Create Configurations
var cfg_saro = DefaultConfig()
var cfg_raya = DefaultConfig()
# Cross pollinate Peers - No Waku discovery is used in this example
2025-12-03 15:49:38 +08:00
# cfg_saro.staticPeers.add(cfg_raya.getMultiAddr())
# cfg_raya.staticPeers.add(cfg_saro.getMultiAddr())
2025-09-28 14:45:02 -07:00
2025-10-09 18:46:36 -07:00
let sKey = loadPrivateKeyFromBytes(@[45u8, 216, 160, 24, 19, 207, 193, 214, 98, 92, 153, 145, 222, 247, 101, 99, 96, 131, 149, 185, 33, 187, 229, 251, 100, 158, 20, 131, 111, 97, 181, 210]).get()
let rKey = loadPrivateKeyFromBytes(@[43u8, 12, 160, 51, 212, 90, 199, 160, 154, 164, 129, 229, 147, 69, 151, 17, 239, 51, 190, 33, 86, 164, 50, 105, 39, 250, 182, 116, 138, 132, 114, 234]).get()
let sIdent = Identity(name: "saro", privateKey: sKey)
2025-09-28 14:45:02 -07:00
# Create Clients
2025-10-09 18:46:36 -07:00
var saro = newClient(cfg_saro, sIdent)
var raya = newClient(cfg_raya, Identity(name: "raya", privateKey: rKey))
2025-09-28 14:45:02 -07:00
2025-10-09 18:46:36 -07:00
var ri = 0
2025-09-28 14:45:02 -07:00
# Wire Callbacks
saro.onNewMessage(proc(convo: Conversation, msg: ReceivedMessage) {.async.} =
echo " Saro <------ :: " & getContent(msg.content)
2025-11-20 16:01:37 -08:00
await sleepAsync(5000.milliseconds)
discard await convo.sendMessage(initTextFrame("Ping").toContentFrame())
2025-10-09 18:46:36 -07:00
2025-09-28 14:45:02 -07:00
)
saro.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} =
echo " Saro -- Read Receipt for " & msgId
)
raya.onNewMessage(proc(convo: Conversation,msg: ReceivedMessage) {.async.} =
echo fmt" ------> Raya :: from:{msg.sender} " & getContent(msg.content)
2025-11-20 16:01:37 -08:00
await sleepAsync(500.milliseconds)
discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame())
2025-11-20 16:01:37 -08:00
await sleepAsync(800.milliseconds)
discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame())
2025-11-20 16:01:37 -08:00
await sleepAsync(500.milliseconds)
discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame())
2025-10-09 18:46:36 -07:00
inc ri
2025-09-28 14:45:02 -07:00
)
raya.onNewConversation(proc(convo: Conversation) {.async.} =
echo " ------> Raya :: New Conversation: " & convo.id()
discard await convo.sendMessage(initTextFrame("Hello").toContentFrame())
2025-09-28 14:45:02 -07:00
)
raya.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} =
echo " raya -- Read Receipt for " & msgId
)
await saro.start()
await raya.start()
2025-11-20 16:01:37 -08:00
await sleepAsync(5.seconds)
2025-09-28 14:45:02 -07:00
# Perform OOB Introduction: Raya -> Saro
let raya_bundle = raya.createIntroBundle()
discard await saro.newPrivateConversation(raya_bundle)
2025-12-03 15:49:38 +08:00
await sleepAsync(200.seconds) # Run for some time
2025-09-28 14:45:02 -07:00
saro.stop()
2025-10-09 18:46:36 -07:00
raya.stop()
when isMainModule:
waitFor main()
notice "Shutdown"