Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
40
Task/Chat-server/Nim/chat-server.nim
Normal file
40
Task/Chat-server/Nim/chat-server.nim
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import asyncnet, asyncdispatch
|
||||
|
||||
type TClient = tuple[socket: AsyncSocket, name: string]
|
||||
var clients: seq[Client] = @[]
|
||||
|
||||
proc sendOthers(client: Client, line: string) {.async.} =
|
||||
for c in clients:
|
||||
if c != client:
|
||||
await c.socket.send(line & "\c\L")
|
||||
|
||||
proc processClient(socket: AsyncSocket) {.async.} =
|
||||
await socket.send("Please enter your name: ")
|
||||
let client: Client = (socket, await socket.recvLine())
|
||||
|
||||
clients.add client
|
||||
discard client.sendOthers "+++ " & client.name & " arrived +++"
|
||||
|
||||
while true:
|
||||
let line = await client.socket.recvLine()
|
||||
if line == "":
|
||||
discard client.sendOthers "--- " & client.name & " leaves ---"
|
||||
break
|
||||
discard client.sendOthers client.name & "> " & line
|
||||
|
||||
for i,c in clients:
|
||||
if c == client:
|
||||
clients.del i
|
||||
break
|
||||
|
||||
proc serve() {.async.} =
|
||||
var server = newAsyncSocket()
|
||||
server.bindAddr(Port(4004))
|
||||
server.listen()
|
||||
|
||||
while true:
|
||||
let socket = await server.accept()
|
||||
discard processClient socket
|
||||
|
||||
discard serve()
|
||||
runForever()
|
||||
Loading…
Add table
Add a link
Reference in a new issue