-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e5d359b
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os, threadpool, asyncnet, asyncdispatch, protocol | ||
|
||
proc connect(socket: AsyncSocket, serverAddr: string) {.async.} = | ||
echo("Connecting to ", serverAddr) | ||
await socket.connect(serverAddr, 7687.Port) | ||
echo("Connected!") | ||
while true: | ||
let line = await socket.recvLine() | ||
let parsed = parseMessage(line) | ||
echo(parsed.username, " said ", parsed.message) | ||
|
||
echo("Chat application started") | ||
|
||
if paramCount() == 0: | ||
quit("Please specify the server address") | ||
|
||
let serverAddr = paramStr(1) | ||
var socket = newAsyncSocket() | ||
|
||
asyncCheck connect(socket, serverAddr) | ||
var messageFlowVar = spawn stdin.readLine() | ||
while true: | ||
if messageFlowVar.isReady(): | ||
let message = createMessage("Anonymous", ^messageFlowVar) | ||
asyncCheck socket.send(message) | ||
messageFlowVar = spawn stdin.readLine() | ||
|
||
asyncdispatch.poll() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
type | ||
Message* = object | ||
username*: string | ||
message*: string | ||
|
||
proc parseMessage*(data: string): Message = | ||
let dataJson = parseJson(data) | ||
result.username = dataJson["username"].getStr() | ||
result.message = dataJson["message"].getStr() | ||
|
||
proc createMessage*(username, message: string): string = | ||
result = $(%{ | ||
"username": %username, | ||
"message": %message | ||
}) & "\c\l" | ||
|
||
when isMainModule: | ||
block: | ||
let data = """{"username": "John", "message": "Hi!"}""" | ||
let parsed = parseMessage(data) | ||
doAssert parsed.username == "John" | ||
doAssert parsed.message == "Hi!" | ||
|
||
block: | ||
let data = """foobar""" | ||
try: | ||
let parsed = parseMessage(data) | ||
doAssert false | ||
except JsonParsingError: | ||
doAssert true | ||
except: | ||
doAssert false | ||
|
||
block: | ||
let expected = """{"username":"dom","message":"hello"}""" & "\c\l" | ||
doAssert createMessage("dom", "hello") == expected | ||
|
||
echo("All tests passed!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters