51 lines
1.3 KiB
Nim
51 lines
1.3 KiB
Nim
|
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
||
|
# SPDX-License-Identifier: Unlicense
|
||
|
|
||
|
import std/[asyncdispatch, asyncfile, posix, random, strutils]
|
||
|
import preserves
|
||
|
import syndicate, syndicate/protocols/schemas/simpleChatProtocol, syndicate/sturdy
|
||
|
|
||
|
randomize()
|
||
|
|
||
|
syndicate chat:
|
||
|
|
||
|
let me = "user_" & $rand(range[10..1000])
|
||
|
|
||
|
spawn "debug":
|
||
|
onAsserted(?s) do (s: Preserve):
|
||
|
echo " asserted ", s
|
||
|
onRetracted(?s) do (s: Preserve):
|
||
|
echo " retracted ", s
|
||
|
onMessage(?s) do (s: Preserve):
|
||
|
echo " message ", s
|
||
|
|
||
|
spawn "log":
|
||
|
during(present(?who)) do (who: string):
|
||
|
echo who, " joined"
|
||
|
onStop:
|
||
|
echo who, " left"
|
||
|
onMessage(says(?who, ?what)) do (who: string; what: string):
|
||
|
echo who, " says ", what
|
||
|
|
||
|
spawn "chat":
|
||
|
asserting present(me)
|
||
|
during (present(me)):
|
||
|
let
|
||
|
inputFacet = getCurrentFacet()
|
||
|
af = newAsyncFile(AsyncFD STDIN_FILENO)
|
||
|
inputFacet.beginExternalTask()
|
||
|
proc readStdin() =
|
||
|
readline(af).addCallback do (f: Future[string]):
|
||
|
if f.failed:
|
||
|
inputFacet.endExternalTask()
|
||
|
else:
|
||
|
callSoon:
|
||
|
readStdin()
|
||
|
let line = read f
|
||
|
if line.len > 0:
|
||
|
let a = says(me, strip line)
|
||
|
send a
|
||
|
readStdin()
|
||
|
|
||
|
waitFor chat()
|