bump version, fix #15

This commit is contained in:
tormund 2020-06-23 00:53:26 +03:00
parent d64a7691ae
commit 2de5efc0cf
4 changed files with 55 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.4" version = "0.5"
author = "Andre von Houck, Volodymyr Melnychuk" author = "Andre von Houck, Volodymyr Melnychuk"
description = "Simple WebSocket library for nim." description = "Simple WebSocket library for nim."
license = "MIT" license = "MIT"

View File

@ -475,6 +475,14 @@ proc sendPing*(ws: WebSocket): Future[void] {.async.} =
proc sendPong(ws: WebSocket): Future[void] {.async.} = proc sendPong(ws: WebSocket): Future[void] {.async.} =
await ws.send("", Opcode.Pong) await ws.send("", Opcode.Pong)
proc sendClose(ws: WebSocket): Future[void] {.async.} =
await ws.send("", Opcode.Close)
proc shutdown*(ws: WebSocket): Future[void] {.async.} =
## close the socket
ws.readyState = Closing
await ws.sendClose
proc receivePacket*(ws: WebSocket): Future[Packet] {.async.} = proc receivePacket*(ws: WebSocket): Future[Packet] {.async.} =
try: try:
## wait for a string packet to come ## wait for a string packet to come
@ -497,7 +505,11 @@ proc receivePacket*(ws: WebSocket): Future[Packet] {.async.} =
return return
elif frame.opcode == Close: elif frame.opcode == Close:
ws.close() if ws.readyState != Closing:
await ws.sendClose()
ws.readyState = Closed
if not ws.transp.isClosed:
ws.transp.close()
except WebSocketError as e: except WebSocketError as e:
raise e raise e

40
tests/test_close.nim Normal file
View File

@ -0,0 +1,40 @@
import news, asyncdispatch, asynchttpserver, asyncnet
proc cb(req: Request): Future[void] {.async.} =
echo "cb ", req
var ws = await newWebsocket(req)
await ws.send("Welcome to simple echo server")
while ws.readyState == Open:
let packet = await ws.receivePacket()
await ws.send(packet)
await ws.shutdown()
await req.respond(Http200, "Hello World")
proc sendMsg() {.async.} =
var ws = await newWebSocket("ws://localhost:9001")
await ws.send("hi")
while ws.readyState == Open:
let str = await ws.receiveString()
echo "received ", str
ws.close()
proc sendClose() {.async.} =
var ws = await newWebSocket("ws://echo.websocket.org:80")
await ws.sendPing()
let pong = await ws.receivePacket()
assert(pong.kind == Pong)
await ws.shutdown()
let close = await ws.receivePacket()
assert(close.kind == Close)
ws.close()
proc run() {.async.} =
var server = newAsyncHttpServer()
asyncCheck server.serve(Port(9001), cb)
await sendMsg()
await sendClose()
server.close()
waitFor run()

View File

@ -3,7 +3,7 @@ import news, asyncdispatch, asynchttpserver
var continueTest = true var continueTest = true
proc establishConnectionAndListen() {.async.} = proc establishConnectionAndListen() {.async.} =
var ws = await newWebSocket("ws://echo.websocket.org:80/") var ws = await newWebSocket("ws://echo.websocket.org:80")
await ws.sendPing() await ws.sendPing()
let pong = await ws.receivePacket() let pong = await ws.receivePacket()
assert(pong.kind == Pong) assert(pong.kind == Pong)