logos-chat/examples/pingpong.nim

88 lines
2.5 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
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
# Create Clients
var saro = newClient(waku_saro).get()
var raya = newClient(waku_raya).get()
2025-09-28 14:45:02 -07:00
# Wire Saro Callbacks
saro.onNewMessage(proc(convo: Conversation, msg: ReceivedMessage) {.async, closure.} =
2025-12-15 13:55:19 -08:00
let contentFrame = msg.content.fromBytes()
notice " Saro <------ ", content = getContent(contentFrame)
await sleepAsync(1000.milliseconds)
2025-12-15 13:55:19 -08:00
discard await convo.sendMessage(initTextFrame("Ping").toContentFrame().toBytes())
)
2025-09-28 14:45:02 -07:00
saro.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} =
notice " Saro -- Read Receipt for ", msgId= msgId
2025-09-28 14:45:02 -07:00
)
# Wire Raya Callbacks
var i = 0
raya.onNewConversation(proc(convo: Conversation) {.async.} =
notice " ------> Raya :: New Conversation: ", id = convo.id()
discard await convo.sendMessage(initTextFrame("Hello").toContentFrame().toBytes())
)
2025-09-28 14:45:02 -07:00
raya.onNewMessage(proc(convo: Conversation,msg: ReceivedMessage) {.async.} =
2025-12-15 13:55:19 -08:00
let contentFrame = msg.content.fromBytes()
notice " ------> Raya :: from: ", content= getContent(contentFrame)
2025-11-20 16:01:37 -08:00
await sleepAsync(500.milliseconds)
discard await convo.sendMessage(initTextFrame("Pong" & $i).toContentFrame().toBytes())
2025-11-20 16:01:37 -08:00
await sleepAsync(800.milliseconds)
discard await convo.sendMessage(initTextFrame("Pang" & $i).toContentFrame().toBytes())
inc i
2025-09-28 14:45:02 -07:00
)
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"