Close doesn't seem to work with WebSocket

I have this simple javascript code:

window.ws = new WebSocket('ws://127.0.0.1:8000/'); ws.onopen = function() { ws.send('hello'); } 

And the server in Ruby is like this:

 require 'em-websocket' class Websocket def run EventMachine.run do EM::WebSocket.start(host: '0.0.0.0', port: '8000') do |ws| ws.onopen do |handshake| puts "Connected" end ws.onclose do puts "Closed" end ws.onmessage do |msg| p msg end end end end end 

When the connection is closed, the server should print “Closed”. In the browser, when I do window.ws.close() , the server does not receive anything, but when I reload the page, it prints a message.

Is there a way to make the client say the connection is closed?

+4
source share
1 answer

I am posting this answer because the problem was discovered to be related to the use of "Docker" and a new question was posted related to the actual problem.

This answer should (hopefully) correctly point out this question since it no longer needs attention so that the community can focus on unanswered questions.

See comments on the question for details.

0
source

All Articles