简体   繁体   English

close似乎不适用于WebSocket

[英]close does not seem to work with WebSocket

I have this simple javascript code : 我有这个简单的JavaScript代码:

window.ws = new WebSocket('ws://127.0.0.1:8000/');

ws.onopen = function() { 
  ws.send('hello');
}

And a server in Ruby like this : Ruby中的服务器如下:

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. 在浏览器中,当我执行window.ws.close() ,服务器没有收到任何内容,但是当我重新加载页面时,它会打印该消息。

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. 我发布了这个答案,因为发现该问题与“Docker”的使用有关,并且发布了一个与实际问题相关的新问题。

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. 有关详细信息,请参阅该问题的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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