53 lines
1.6 KiB
Nim
53 lines
1.6 KiB
Nim
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/[asyncdispatch, json, sequtils, streams, tables, uri]
|
|
import preserves, syndicate
|
|
import ws
|
|
|
|
import private/jsonhooks
|
|
|
|
type
|
|
Args {.preservesDictionary.} = object
|
|
dataspace: Ref
|
|
url: string
|
|
|
|
Internal* {.preservesRecord: "internal".} = object
|
|
dataspace: Ref
|
|
|
|
proc recvMessages(facet: Facet; ds: Ref; ws: WebSocket) =
|
|
var fut: Future[string]
|
|
proc recvMessage() {.gcsafe.} =
|
|
fut = receiveStrPacket ws
|
|
addCallback(fut, facet) do (turn: var Turn):
|
|
message(turn, ds, fut.read.parseJson)
|
|
recvMessage()
|
|
recvMessage()
|
|
|
|
proc bootClient(turn: var Turn; ds: Ref; ws: WebSocket) =
|
|
let dumpStream = openFileStream("/tmp/simplex_bot_actor.log", fmWrite)
|
|
let intern = newDataspace(turn)
|
|
discard publish(turn, ds, Internal(dataspace: intern))
|
|
recvMessages(turn.facet, intern, ws)
|
|
onMessage(turn, intern, grab()) do (msg: Assertion):
|
|
writeText(dumpStream, msg)
|
|
write(dumpStream, '\n')
|
|
flush(dumpStream)
|
|
|
|
proc boot*(root: Ref; turn: var Turn) =
|
|
during(turn, root, ?Args) do (dataspace: Ref, url: string):
|
|
debugEcho "got dataspace ", dataspace, " and URL ", url
|
|
var ws: WebSocket
|
|
newWebSocket(url).addCallback(turn) do (turn: var Turn; s: WebSocket):
|
|
ws = s
|
|
debugecho "connected to ", url
|
|
bootClient(turn, dataspace, ws)
|
|
do:
|
|
close ws
|
|
|
|
when isMainModule:
|
|
runActor("eris_actor") do (root: Ref; turn: var Turn):
|
|
# connectStdio(root, turn)
|
|
boot(root, turn)
|
|
discard publish(turn, root, Args(dataspace: root, url: "ws://127.0.0.1:5225/"))
|