close does not seem to work with WebSocket

非 Y 不嫁゛ 提交于 2020-01-03 17:48:38

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31857811/close-does-not-seem-to-work-with-websocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!