Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket#close accepts code and reason arguments #7483

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/http/web_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ class HTTP::WebSocket
end
end

def close(message = nil)
# Closes the websocket with *reason* and *code*.
def close(reason : String? = nil, code : Int16? = nil)
vladfaust marked this conversation as resolved.
Show resolved Hide resolved
return if closed?
@closed = true
@ws.close(message)
@ws.close(reason, code)
end

def run
Expand Down
15 changes: 14 additions & 1 deletion src/http/web_socket/protocol.cr
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,20 @@ class HTTP::WebSocket::Protocol
end
end

def close(message = nil)
def close(reason : String? = nil, code : Int16? = nil)
if code
raw = uninitialized UInt8[2]
IO::ByteFormat::BigEndian.encode(code, raw.to_slice)

message = String.new(raw)
vladfaust marked this conversation as resolved.
Show resolved Hide resolved

if reason
message += reason
end
else
message = reason
end

if message
send(message.to_slice, Opcode::CLOSE)
else
Expand Down