简体   繁体   中英

close does not 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 a server in Ruby 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 a connection is close, the server should print "Closed". In the browser, when I do window.ws.close() , nothing is received by the server, but when I reload the page, it print the message.

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

I'm posting this answer since the issue was discovered to be related to the usage of 'Docker' and a new question was posted relating to the actual issue.

This answer should (hopefully) correctly mark this question as no longer in need of attention, so that the community can focus on unanswered questions.

See the comments to the question for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM