【问题标题】:Padrino websockets + Heroku; Connection closed before receiving a handshake responsePadrino websockets + Heroku;在收到握手响应之前连接已关闭
【发布时间】:2015-12-18 23:34:45
【问题描述】:

我正在使用 padrino websockets (https://github.com/dariocravero/padrino-websockets) 为我的网站提供聊天系统,它在我的本地机器上运行良好。但是,在部署到 heroku(免费)后,websocket 不会建立连接并会返回

failed: Connection closed before receiving a handshake response

它在本地主机上运行良好,我使用它来连接:

connection = new WebSocket('ws://localhost:3000/channel');

但是,当在 heroku 上使用时:

connection = new WebSocket('ws://******.herokuapp.com:3000/channel');

它返回一个握手错误(上图)

我的实现服务器端

websocket :channel do
  on :newmessage do |message|
    currentAccount = Account.find_by(lastLoginIP: message["ip"]) rescue nil

    if currentAccount != nil
      broadcast :channel, {
        "name" => currentAccount.nickname,
        "url" => currentAccount.url,
        "image" =>  currentAccount.image,
        "chatmessage" => message["chatmessage"][0..80]
        }
    end

  end
end

在我的主要 Padrino app.rb 中,在我的 Procfile 中。怎么回事?

web: bundle exec puma -t 1:16 -p ${PORT:-3000} -e ${RACK_ENV:-production}

【问题讨论】:

    标签: heroku websocket sinatra padrino


    【解决方案1】:

    您的 Websocket 端口 (3000) 未在 Heroku 上公开。

    Heroku 将向端口 80 或端口 443 发出的任何请求转发到 Web dyno 的动态端口,存储在 $PORT bash 变量中。

    在您的浏览器(客户端)中,尝试替换此行:

     connection = new WebSocket('ws://localhost:3000/channel');
    

    用这一行:

     connection = new WebSocket('ws://' + window.document.location.host + 'channel');
    

    或者,如果您想同时支持 SSL 和未加密的 Websocket:

     ws_uri = (window.location.protocol.match(/https/) ? 'wss' : 'ws') +
              '://' + window.document.location.host + 'channel';
     connection = new WebSocket(ws_uri)
    

    如果您的应用程序和 websocket 层共享同一个服务器,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2016-06-21
      相关资源
      最近更新 更多