Chat server: Difference between revisions

Content added Content deleted
Line 1,162: Line 1,162:
<lang nimrod>import asyncnet, asyncdispatch
<lang nimrod>import asyncnet, asyncdispatch


type TClient = tuple[socket: PAsyncSocket, name: string]
type TClient = tuple[socket: AsyncSocket, name: string]
var clients: seq[TClient] = @[]
var clients: seq[Client] = @[]


proc sendOthers(client: TClient, line: string) {.async.} =
proc sendOthers(client: Client, line: string) {.async.} =
for c in clients:
for c in clients:
if c != client:
if c != client:
await c.socket.send(line & "\c\L")
await c.socket.send(line & "\c\L")


proc processClient(socket: PAsyncSocket) {.async.} =
proc processClient(socket: AsyncSocket) {.async.} =
await socket.send("Please enter your name: ")
await socket.send("Please enter your name: ")
let client: TClient = (socket, await socket.recvLine())
let client: Client = (socket, await socket.recvLine())


clients.add client
clients.add client
Line 1,191: Line 1,191:
proc serve() {.async.} =
proc serve() {.async.} =
var server = newAsyncSocket()
var server = newAsyncSocket()
server.bindAddr(TPort(4004))
server.bindAddr(Port(4004))
server.listen()
server.listen()