Skip to content

Commit

Permalink
Merge pull request #60 from jebej/master
Browse files Browse the repository at this point in the history
throw error if client disconnects while reading
  • Loading branch information
shashi authored May 16, 2017
2 parents 18a88a0 + 7ffa0a7 commit eb16f72
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const TCPSock = Base.TCPSocket
""" Buffer writes to socket till flush (sock)"""
init_socket(sock) = Base.buffer_writes(sock)


type WebSocketClosedError <: Exception end
Base.showerror(io::IO, e::WebSocketClosedError) = print(io, "Error: client disconnected")

"""
A WebSocket is a wrapper over a TcpSocket. It takes care of wrapping outgoing
data in a frame and unwrapping (and concatenating) incoming data.
Expand Down Expand Up @@ -248,11 +252,13 @@ is_control_frame(msg::WebSocketFragment) = (msg.opcode & 0b0000_1000) > 0

""" Respond to pings, ignore pongs, respond to close."""
function handle_control_frame(ws::WebSocket,wsf::WebSocketFragment)

if wsf.opcode == OPCODE_CLOSE
# Reply with an empty CLOSE frame
locked_write(ws.socket, true, "", OPCODE_CLOSE)
ws.is_closed = true
wait(ws.socket.closenotify)
close(ws.socket)
throw(WebSocketClosedError())
elseif wsf.opcode == OPCODE_PING
write_pong(ws.socket,wsf.data)
elseif wsf.opcode == OPCODE_PONG
Expand Down

0 comments on commit eb16f72

Please sign in to comment.