Websocket connection closes without opening on Shotgun server

I have a Sinatra application that uses Websockets.
My application works when I launch it using ruby app.rb, but not when I try to launch it using shotgun app.rb.

This is in my send_out.erb :

<script>
$(document).ready(function(){
connection = new WebSocket('ws://' + window.location.host + window.location.pathname);
connection.onopen = function(){
    $("#msgs").append('Connection opened'+"<br>")
};
connection.onmessage = function(e){
    $("#msgs").append(e.data+"<br>");
};
connection.onclose = function() {
    $("#msgs").append('Connection closes from view'+"<br>");
};
$("form").submit(function(){
    connection.send( $("input").val() );
});
});
</script>

And this is in my app.rb :

require 'sinatra-websocket'
set :sockets, []
get '/sending_out' do
request.websocket do |connection|
  connection.onopen do
    connection.send("Hello World!")
    settings.sockets << connection

    connection.send("opened")
    connection.send("went")

  end
  connection.onmessage do |msg|
    EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
  end

  connection.onclose do
    warn("websocket closed")
    settings.sockets.delete(ws)
  end
end
end

He must show

Connection opened
Hello World!
opened
went

when I go to the page. But he only shows

Connection closes from view

with a shotgun .

And the console says that the connection to WebSocket with 'ws: //127.0.0.1: 9393 / send_out' failed: WebSocket handshake error: unexpected response code: 500 .

Is there a problem starting Websockets with Shotgun?

+4

All Articles