Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Created October 24, 2011 22:01
Show Gist options
  • Save ThisIsMissEm/1310463 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/1310463 to your computer and use it in GitHub Desktop.
Chrome "WebSocket is closed before the connection is established." Test Case

Chrome "WebSocket is closed before the connection is established." Test Case

Install dependencies

bundle install

Start simple websocket server

ruby ws-server.rb

In another terminal start web

ruby app.rb

To reproduce

  • Open http://localhost:4567/
  • Verify that the following error is logged to the javascript console:
    • "WebSocket is closed before the connection is established."
  • Verify that none of the following have been logged:
    • "onerror"
    • "window.onerror"
    • "try/catch around ws.close()"
    • "try/catch around entire ws code."
require 'bundler/setup'
require 'sinatra'
set :views, settings.root
get '/' do
erb :index
end
source :rubygems
gem 'em-websocket'
gem 'sinatra'
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
em-websocket (0.3.5)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
eventmachine (0.12.10)
rack (1.3.2)
sinatra (1.2.3)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
tilt (1.3.3)
PLATFORMS
ruby
DEPENDENCIES
em-websocket
sinatra
<!DOCTYPE html>
<head>
<title>Chrome "WebSocket is closed before the connection is established." Test Case</title>
</head>
<body>
<h1>Chrome "WebSocket is closed before the connection is established." Test Case</h1>
<p>This page opens a websocket connection to ws://localhost:8080/</p>
<h2>Running this Test Case</h2>
<h3>Install dependencies:</h3>
<pre><code>bundle install</code></pre>
<h3>Start simple websocket server:</h3>
<pre><code>ruby ws-server.rb</code></pre>
<h3>Start webserver:</h3>
<pre><code>ruby app.rb</code></pre>
<h3>Repeat the following steps:</h3>
<ul>
<li>Open <a href="http://localhost:4567/">http://localhost:4567/</a></li>
<li>Open the JavaScript console.</li>
<li>Click the "test this" button at the bottom of the page</li>
<li>Verify that the following error is logged to the javascript console:</li>
<li><code>"WebSocket is closed before the connection is established."</code></li>
<li>Verify that none of the following have been logged:
<ul>
<li>&#8220;onerror&#8221;</li>
<li>&#8220;window.onerror&#8221;</li>
<li>&#8220;try/catch around ws.close()&#8221;</li>
<li>&#8220;try/catch around entire ws code.&#8221;</li>
</ul></li>
</ul>
<hr />
<button id="test">Test this</button>
<script type="text/javascript">
window.onerror = function() {
console.log('window.onerror')
}
document.getElementById('test').onclick = function() {
var Socket = window['MozWebSocket'] || window['WebSocket']
if (Socket) {
try {
var ws = new Socket('ws://localhost:8080/')
ws.onopen = function() { console.log('onopen'); };
ws.onerror = function() { console.log('onerror attached to websocket object.'); };
ws.onclose = function() { console.log('onclose'); };
ws.onmessage = function(evt) { console.log('onmessage: '+evt.data); };
// Close as soon as open is called.
try {
ws.onopen = null;
ws.onmessage = null;
ws.onclose = null;
ws.onerror = null;
ws.close();
} catch (e) {
console.log('try/catch around ws.close()')
}
} catch (e) {
console.log('try/catch around entire ws code.')
}
}
}
</script>
</body>
require 'bundler/setup'
require 'em-websocket'
i = 0
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
i += 1
ws.onopen { puts "Opened #{i}"}
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "Closed #{i}" }
ws.onerror { |e| puts "Error: #{e.message}" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment