我有一个使用 Websockets 的 Sinatra 应用程序。
我的应用程序在我ruby app.rb
使用shotgun app.rb
.
这是在我的sent_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 connection to 'ws://127.0.0.1:9393/sending_out' failed: Error during WebSocket handshake: Unexpected response code: 500。
使用 Shotgun 运行 Websocket 是否存在问题?