Skip to content

Instantly share code, notes, and snippets.

@jrfondren
Created May 15, 2019 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrfondren/bbbc3a7eb3c30d3485a3e2913557d3d2 to your computer and use it in GitHub Desktop.
Save jrfondren/bbbc3a7eb3c30d3485a3e2913557d3d2 to your computer and use it in GitHub Desktop.
ways to handle local IO
import asyncnet, asyncdispatch, threadpool
proc echoStdin =
var line = spawn stdin.readLine()
while true:
if line.isReady():
echo "You said: " & ^line
line = spawn stdin.readLine()
asyncdispatch.drain(60)
proc accept {.async.} =
var serv = newAsyncSocket()
serv.setSockOpt(OptReuseAddr, true)
serv.bindAddr(Port(1079))
serv.listen()
while true:
let client = await serv.accept()
discard
asyncCheck accept()
echoStdin()
import asyncnet, asyncdispatch, threadpool
proc echoStdin =
var line = spawn stdin.readLine()
addTimer(120, false, proc (fd: AsyncFD): bool {.closure, gcsafe.} =
if line.isReady():
echo "You said: " & ^line
line = spawn stdin.readLine()
)
proc accept {.async.} =
var serv = newAsyncSocket()
serv.setSockOpt(OptReuseAddr, true)
serv.bindAddr(Port(1079))
serv.listen()
while true:
let client = await serv.accept()
echo "Got a client"
discard
echoStdin()
asyncCheck accept()
runForever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment