2014-10-27 148 views
4

我有一個使用Websocket的Sinatra應用程序。
我的應用程序工作時,我運行它與ruby app.rb,但不是當我試圖與shotgun app.rb運行它。Websocket連接在Shotgun服務器上未打開時關閉

這是我sending_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> 

這是我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 

它必須顯示

Connection opened 
Hello World! 
opened 
went 

時我去了頁面。但是,這隻能說明

Connection closes from view 

獵槍

並且在控制檯中它說WebSocket連接到'ws://127.0.0.1:9393/sending_out'失敗:在WebSocket握手期間出錯:意外的響應代碼:500

使用Shotgun運行Websockets是否存在問題?

回答

相關問題