JuliaWeb / WebSockets.jl

A WebSockets library for Julia
MIT License
157 stars 58 forks source link

How to get status code from WebSocketClosedError? #176

Closed tbenst closed 2 years ago

tbenst commented 2 years ago

I'm trying to debug why all my websockets keep crashing at the same time. I'm subscribed to a feed.

I get the following error: Debug: WebSocketClosedError("while read(ws|client) Client side closed socket connection - Performed closing handshake.")

I believe this comes from https://github.com/JuliaWeb/WebSockets.jl/blob/0e81f48ab73dcb0d29df3d04539f922f2d4ae10e/src/WebSockets.jl#L480 which in turn is triggered by https://github.com/JuliaWeb/WebSockets.jl/blob/0e81f48ab73dcb0d29df3d04539f922f2d4ae10e/src/WebSockets.jl#L388-L394

It seems like the issue is on the Julia (client) side rather than on the webserver. Any tips for how I might debug? Thank you!

hustf commented 2 years ago

I can see how this is confusing. We have two subtypes of WebSockets. The 'read' function's argument is a WebSocket of the 'client' type. What that means is ambigious - which side of the connection is a client? Are we reading from our end or from the other end? Enabling more debug output will hopefully set that straight.

We know from the message that WebSockets.jl had WebSocketError 1002 or 1006 (the definition set is shown in the code). Either, the other side did something not according to protocol or the connection was lost. To play nice, our side initiated a separate task which will be trying to close the connection in the proper way during the following TIMEOUT_CLOSEHANDSHAKE seconds.

To get details from @debug statements, the simplest way is from the command line: Users\f> julia -g 2

If you want full clarity, we have an additional log level, @wslog. See, ´examples/count_with_logger.jl´.

I suppose you have been calling the 'readguarded' function? Use 'read' instead for full debugging sessions - most closing events are normal timeouts from a browser (i.e. from a client) or from a server, and some don't follow the protocol while closing.

tbenst commented 2 years ago

@hustf thank you! That's super helpful. And yes indeed, I've been using readguarded and just switched to read.

It turns out there were two issues:

1) I was writing malformed messages. This led to Debug: ErrorException("WebSocket{MbedTLS.SSLContext} does not support byte I/O") error

2) I forgot my heardbeat message was commented out. As such, the connection was unceremoniously dropping, and I got a 1006 error.

issue solved, many thanks!!