close 似乎不适用于 WebSocket [英] close does not seem to work with WebSocket

查看:71
本文介绍了close 似乎不适用于 WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的 javascript 代码:

I have this simple javascript code :

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

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

还有一个像这样的 Ruby 服务器:

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

当连接关闭时,服务器应该打印Closed".在浏览器中,当我执行 window.ws.close() 时,服务器没有收到任何信息,但是当我重新加载页面时,它会打印消息.

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?

推荐答案

我发布此答案是因为发现该问题与Docker"的使用有关,并且发布了一个与实际问题相关的新问题.

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.

这篇关于close 似乎不适用于 WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆