Skip to content

Commit fce2bf6

Browse files
committed
update pingpong example
1 parent 75a08ed commit fce2bf6

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ nim_chat_poc: | build-waku-librln build-waku-nat nim_chat_poc.nims
8888
$(ENV_SCRIPT) nim nim_chat_poc $(NIM_PARAMS) nim_chat_poc.nims
8989

9090
# Ensure there is a nimble task with a name that matches the target
91-
tui bot_echo: | build-waku-librln build-waku-nat nim_chat_poc.nims
91+
tui bot_echo pingpong: | build-waku-librln build-waku-nat nim_chat_poc.nims
9292
echo -e $(BUILD_MSG) "build/$@" && \
9393
$(ENV_SCRIPT) nim $@ $(NIM_PARAMS) --path:src nim_chat_poc.nims
9494

examples/pingpong.nim

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import chronicles
22
import chronos
3+
import strformat
34

45
import chat_sdk
56
import content_types
67

8+
# TEsting
9+
import ../src/chat_sdk/crypto
10+
711

812
proc getContent(content: ContentFrame): string =
913
# Skip type checks and assume its a TextFrame
1014
let m = decode(content.bytes, TextFrame).valueOr:
11-
raise newException(ValueError, fmt"Badly formed Content")
12-
return fmt"{m}"
15+
raise newException(ValueError, fmt"Badly formed Content")
16+
return fmt"{m}"
1317

1418
proc main() {.async.} =
1519

@@ -21,15 +25,22 @@ proc main() {.async.} =
2125
cfg_saro.staticPeers.add(cfg_raya.getMultiAddr())
2226
cfg_raya.staticPeers.add(cfg_saro.getMultiAddr())
2327

28+
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()
29+
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()
30+
31+
let sIdent = Identity(name: "saro", privateKey: sKey)
32+
2433
# Create Clients
25-
var saro = newClient(cfg_saro, createIdentity("Saro"))
26-
var raya = newClient(cfg_raya, createIdentity("Raya"))
34+
var saro = newClient(cfg_saro, sIdent)
35+
var raya = newClient(cfg_raya, Identity(name: "raya", privateKey: rKey))
2736

37+
var ri = 0
2838
# Wire Callbacks
2939
saro.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} =
3040
echo " Saro <------ :: " & getContent(msg)
31-
await sleepAsync(10000)
41+
await sleepAsync(5000)
3242
discard await convo.sendMessage(saro.ds, initTextFrame("Ping").toContentFrame())
43+
3344
)
3445

3546
saro.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} =
@@ -41,8 +52,13 @@ proc main() {.async.} =
4152

4253
raya.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} =
4354
echo " ------> Raya :: " & getContent(msg)
44-
await sleepAsync(10000)
45-
sdiscard await convo.sendMessage(raya.ds, initTextFrame("Pong").toContentFrame())
55+
await sleepAsync(500)
56+
discard await convo.sendMessage(raya.ds, initTextFrame("Pong" & $ri).toContentFrame())
57+
await sleepAsync(800)
58+
discard await convo.sendMessage(raya.ds, initTextFrame("Pong" & $ri).toContentFrame())
59+
await sleepAsync(500)
60+
discard await convo.sendMessage(raya.ds, initTextFrame("Pong" & $ri).toContentFrame())
61+
inc ri
4662
)
4763

4864
raya.onNewConversation(proc(convo: Conversation) {.async.} =
@@ -63,7 +79,12 @@ proc main() {.async.} =
6379
let raya_bundle = raya.createIntroBundle()
6480
discard await saro.newPrivateConversation(raya_bundle)
6581

66-
await sleepAsync(10000) # Run for some time
82+
await sleepAsync(20000) # Run for some time
6783

6884
saro.stop()
69-
raya.stop()
85+
raya.stop()
86+
87+
88+
when isMainModule:
89+
waitFor main()
90+
notice "Shutdown"

nim_chat_poc.nimble

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ task tui, "Build Waku based simple example":
4949
task bot_echo, "Build the EchoBot example":
5050
let name = "bot_echo"
5151
buildBinary name, "examples/", "-d:chronicles_log_level='INFO' -d:chronicles_disabled_topics='waku node' "
52+
53+
task pingpong, "Build the Pingpong example":
54+
let name = "pingpong"
55+
buildBinary name, "examples/", "-d:chronicles_log_level='INFO' -d:chronicles_disabled_topics='waku node' "

0 commit comments

Comments
 (0)