Open moigagoo opened 6 years ago
How did you "stop the client"? With Ctrl+C? Did you do ws.sock.close()
? The way to stop it that would have worked in this case is ws.close()
on the client so the server receives the Close opcode.
The problem in this case is how the library treats closed sockets that haven't given a Close opcode which is currently with an IOError. To get around it you'd do this:
try:
let (opcode, data) = await ws.readData()
echo "(opcode: ", opcode, ", data: ", data, ")"
except IOError, ProtocolError:
ws.sock.close()
echo "closing websocket because of error: ", getCurrentExceptionMsg()
Or this:
let (opcode, data) =
try:
await ws.readData()
except:
ws.sock.close()
(Opcode.Close, "")
echo "(opcode: ", opcode, ", data: ", data, ")"
But the question remains if the library should either:
If we're gonna keep exceptions which we probably are, I prefer the last one as using IOError everywhere is probably not a good idea
Yes, I stop the client with Ctrl+C, which is not a valid socket close event. I think, however, the server should be tolerant to that, so a fix in the sample server code will be enough to get rid of the confusion.
As a workaround, I wrapped my code in a try block, but your solutions are cleaner, I especially like the second one.
On a general note, maybe a switch from nil- and exception-based flow to using options will make the lib interface better?
Parts of your stack trace look similar to a problem I was having in #52 and put a fix in #55. You might want to get HEAD and see if you still have this issue.
websocket.nim version: 0.3.3 Nim version: 0.18.0, devel
Create a server and client from the docs samples:
Compile and run the server and client.
Observe as they exchange data.
Stop the client.
Expected behavior: the server continues running Actual behavior: the server craches with this trace: