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

Unable to close websocket from the on_message handler #624

Open
AlexandriaOL opened this issue Dec 25, 2021 · 3 comments
Open

Unable to close websocket from the on_message handler #624

AlexandriaOL opened this issue Dec 25, 2021 · 3 comments
Labels

Comments

@AlexandriaOL
Copy link

Description

I am unable to close a socket from the message handler

Steps to Reproduce

require "kemal"

ws "/" do |sock, context|
  sock.on_message do |message|
    puts message
    sock.close HTTP::WebSocket::CloseCode::NormalClosure
  end
  
  sock.on_close do
    puts "Waah"
  end
end

Kemal.run

Expected behavior:

It should close the websocket after a message is recieved

Actual behavior:

The websocket stays open and can continue sending messages

Reproduces how often:

continually

Versions

aster@hephaestus:redacted$ crystal --version
Crystal 1.2.1 [4e6c0f26e] (2021-10-21)

LLVM: 10.0.0
Default target: x86_64-unknown-linux-gnu
@sdogruyol sdogruyol added the bug label Jan 5, 2022
@a-alhusaini
Copy link
Contributor

Is this actually an issue in Kemal or Crystal's HTTP::WebSocketHandler?

@a-alhusaini
Copy link
Contributor

on_close is only triggerred if a websocket connection is closed from the othe side. Since your server is the one that closed it. The on_close handler will never get triggerred.

If the client were to close the connection. on_close would be triggerred on the server side.

@straight-shoota
Copy link
Contributor

To clarify: on_close listens for a WebSocket close event. This is a specific event that must be explicitly sent from the other end. It's not implicit when for example the underlying connection closes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants