【问题标题】:Websocket connection closes without opening on Shotgun serverWebsocket 连接关闭但未在 Shotgun 服务器上打开
【发布时间】: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


    【解决方案1】:

    Shotgun 的主要功能是它会在每次请求时自动重新加载您的整个代码,我认为这也可能是您面临的问题。

    Shotgun 应该只用于开发。

    出于生产目的,您还有很多其他可用选项:

    ruby 服务器的比较可以在https://www.digitalocean.com/community/tutorials/a-comparison-of-rack-web-servers-for-ruby-web-applications找到

    【讨论】:

    • 我不知道它是否会起作用,因为我已经没有此代码,但如果有人遇到我的问题并且通过此答案解决了,请告诉我,以便我可以接受它。
    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 2018-11-09
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多