Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save enthus1ast/19a472de473e1049ec954f9cc946068c to your computer and use it in GitHub Desktop.
Save enthus1ast/19a472de473e1049ec954f9cc946068c to your computer and use it in GitHub Desktop.
import asyncdispatch, asyncnet
type
Server = ref object
socket: AsyncSocket
proc newServer(): Server =
Server(socket: newAsyncSocket())
proc loop(server: Server, port = 7920) {.async.} =
server.socket.bindAddr(Port(port), "0.0.0.0")
server.socket.listen()
while true:
let (netAddr, clientSocket) = await server.socket.acceptAddr()
# echo await clientSocket.recvLine()
await clientSocket.send("HTTP/1.1 200 OK\n\nTest")
clientSocket.close()
var server = newServer()
echo "Staring server..."
waitFor loop(server)
###################################################
[david@eb fit]$ ncat 127.0.0.1 7920
HTTP/1.1 200 OK
Test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment