mirror of
https://github.com/logos-messaging/nim-chat-poc.git
synced 2026-01-08 00:53:08 +00:00
Add external handler
This commit is contained in:
parent
c857cb05b1
commit
c19738c0d8
@ -1,13 +1,19 @@
|
|||||||
import
|
import
|
||||||
|
chronicles,
|
||||||
chronos
|
chronos
|
||||||
|
|
||||||
import
|
import
|
||||||
waku_client
|
waku_client
|
||||||
|
|
||||||
|
|
||||||
|
proc handleMessages(pubsubTopic: string, message: seq[byte]): Future[
|
||||||
|
void] {.gcsafe, raises: [Defect].} =
|
||||||
|
info "ClientRecv", pubTopic = pubsubTopic, msg = message
|
||||||
|
|
||||||
proc main(): Future[void] {.async.} =
|
proc main(): Future[void] {.async.} =
|
||||||
echo "Starting POC"
|
echo "Starting POC"
|
||||||
let cfg = DefaultConfig()
|
let cfg = DefaultConfig()
|
||||||
let client = initWakuClient(cfg)
|
let client = initWakuClient(cfg, @[PayloadHandler(handleMessages)])
|
||||||
await client.start()
|
await client.start()
|
||||||
echo "End of POC"
|
echo "End of POC"
|
||||||
|
|
||||||
|
|||||||
@ -27,6 +27,10 @@ const
|
|||||||
FilterContentTopic = ContentTopic("/chatsdk/test/proto")
|
FilterContentTopic = ContentTopic("/chatsdk/test/proto")
|
||||||
|
|
||||||
|
|
||||||
|
type PayloadHandler* = proc(pubsubTopic: string, message: seq[byte]): Future[void] {.
|
||||||
|
gcsafe, raises: [Defect]
|
||||||
|
.}
|
||||||
|
|
||||||
type WakuConfig* = object
|
type WakuConfig* = object
|
||||||
port*: uint16
|
port*: uint16
|
||||||
clusterId*: uint16
|
clusterId*: uint16
|
||||||
@ -37,6 +41,7 @@ type
|
|||||||
WakuClient* = ref object
|
WakuClient* = ref object
|
||||||
cfg: WakuConfig
|
cfg: WakuConfig
|
||||||
node*: WakuNode
|
node*: WakuNode
|
||||||
|
handlers: seq[PayloadHandler]
|
||||||
|
|
||||||
|
|
||||||
proc DefaultConfig*(): WakuConfig =
|
proc DefaultConfig*(): WakuConfig =
|
||||||
@ -102,8 +107,8 @@ proc buildWakuNode(cfg: WakuConfig): WakuNode =
|
|||||||
|
|
||||||
result = node
|
result = node
|
||||||
|
|
||||||
proc messagePushHandler(
|
proc messageHandler(client: WakuClient, pubsubTopic: PubsubTopic,
|
||||||
pubsubTopic: PubsubTopic, message: WakuMessage
|
message: WakuMessage
|
||||||
) {.async, gcsafe.} =
|
) {.async, gcsafe.} =
|
||||||
let payloadStr = string.fromBytes(message.payload)
|
let payloadStr = string.fromBytes(message.payload)
|
||||||
notice "message received",
|
notice "message received",
|
||||||
@ -113,6 +118,10 @@ proc messagePushHandler(
|
|||||||
timestamp = message.timestamp
|
timestamp = message.timestamp
|
||||||
|
|
||||||
|
|
||||||
|
for handler in client.handlers:
|
||||||
|
discard handler(pubsubTopic, message.payload)
|
||||||
|
|
||||||
|
|
||||||
proc taskKeepAlive(client: WakuClient) {.async.} =
|
proc taskKeepAlive(client: WakuClient) {.async.} =
|
||||||
let peer = parsePeerInfo(StaticPeer).get()
|
let peer = parsePeerInfo(StaticPeer).get()
|
||||||
while true:
|
while true:
|
||||||
@ -154,19 +163,20 @@ proc start*(client: WakuClient) {.async.} =
|
|||||||
quit(1)
|
quit(1)
|
||||||
|
|
||||||
client.node.peerManager.start()
|
client.node.peerManager.start()
|
||||||
client.node.wakuFilterClient.registerPushHandler(messagePushHandler)
|
|
||||||
|
|
||||||
let topic = PubsubTopic(client.cfg.pubsubTopic)
|
|
||||||
|
|
||||||
let subscription: SubscriptionEvent = (kind: PubsubSub, topic:
|
let subscription: SubscriptionEvent = (kind: PubsubSub, topic:
|
||||||
client.cfg.pubsubTopic)
|
client.cfg.pubsubTopic)
|
||||||
|
|
||||||
let res = subscribe(client.node, subscription, messagePushHandler)
|
let msg_handler = proc(pubsubTopic: PubsubTopic,
|
||||||
|
message: WakuMessage) {.async, gcsafe.} = discard client.messageHandler(
|
||||||
|
pubsubTopic, message)
|
||||||
|
|
||||||
|
let res = subscribe(client.node, subscription, msg_handler)
|
||||||
if res.isErr:
|
if res.isErr:
|
||||||
error "Subscribe failed", err = res.error
|
error "Subscribe failed", err = res.error
|
||||||
|
|
||||||
await allFutures(taskKeepAlive(client), taskPublishDemo(client))
|
await allFutures(taskKeepAlive(client), taskPublishDemo(client))
|
||||||
|
|
||||||
|
proc initWakuClient*(cfg: WakuConfig, handlers: seq[
|
||||||
proc initWakuClient*(cfg: WakuConfig): WakuClient =
|
PayloadHandler]): WakuClient =
|
||||||
result = WakuClient(cfg: cfg, node: buildWakuNode(cfg))
|
result = WakuClient(cfg: cfg, node: buildWakuNode(cfg), handlers: handlers)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user