nim-chat-poc/examples/pingpong.nim

94 lines
3.1 KiB
Nim
Raw Permalink 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-12-15 13:55:19 -08:00
raise newException(ValueError, fmt"Badly formed ContentType")
2025-10-09 18:46:36 -07:00
return fmt"{m}"
2025-09-28 14:45:02 -07:00
2025-12-15 13:55:19 -08:00
proc toBytes(content: ContentFrame): seq[byte] =
encode(content)
proc fromBytes(bytes: seq[byte]): ContentFrame =
decode(bytes, ContentFrame).valueOr:
raise newException(ValueError, fmt"Badly formed Content")
2025-09-28 14:45:02 -07:00
proc main() {.async.} =
# Create Configurations
var waku_saro = initWakuClient(DefaultConfig())
var waku_raya = initWakuClient(DefaultConfig())
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()
2025-09-28 14:45:02 -07:00
# Create Clients
var saro = newClient(waku_saro, Identity(name: "saro", privateKey: sKey))
var raya = newClient(waku_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.} =
2025-12-15 13:55:19 -08:00
let contentFrame = msg.content.fromBytes()
echo " Saro <------ :: " & getContent(contentFrame)
2025-11-20 16:01:37 -08:00
await sleepAsync(5000.milliseconds)
2025-12-15 13:55:19 -08:00
discard await convo.sendMessage(initTextFrame("Ping").toContentFrame().toBytes())
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.} =
2025-12-15 13:55:19 -08:00
let contentFrame = msg.content.fromBytes()
echo fmt" ------> Raya :: from:{msg.sender} " & getContent(contentFrame)
2025-11-20 16:01:37 -08:00
await sleepAsync(500.milliseconds)
2025-12-15 13:55:19 -08:00
discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame().toBytes())
2025-11-20 16:01:37 -08:00
await sleepAsync(800.milliseconds)
2025-12-15 13:55:19 -08:00
discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame().toBytes())
2025-11-20 16:01:37 -08:00
await sleepAsync(500.milliseconds)
2025-12-15 13:55:19 -08:00
discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame().toBytes())
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()
2025-12-15 13:55:19 -08:00
discard await convo.sendMessage(initTextFrame("Hello").toContentFrame().toBytes())
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-12-03 17:44:41 +08:00
await sleepAsync(10.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, initTextFrame("Init").toContentFrame().toBytes())
2025-09-28 14:45:02 -07:00
2025-12-03 17:34:58 +08:00
await sleepAsync(20.seconds) # Run for some time
2025-09-28 14:45:02 -07:00
2025-12-03 17:44:41 +08:00
await saro.stop()
await raya.stop()
2025-10-09 18:46:36 -07:00
when isMainModule:
waitFor main()
notice "Shutdown"