【发布时间】:2014-10-27 15:15:16
【问题描述】:
我有一个使用 Websockets 的 Sinatra 应用程序。
当我使用ruby app.rb 运行我的应用程序时,它可以工作,但当我尝试使用shotgun app.rb 运行它时却不能。
这是我的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 connection to 'ws://127.0.0.1:9393/sending_out' failed: Error during WebSocket handshake: Unexpected response code: 500。
使用 Shotgun 运行 Websocket 是否存在问题?
【问题讨论】:
标签: ruby websocket sinatra shotgun