问题
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