Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Heroku Padrino websockets+;希罗库;连接在收到握手响应之前关闭_Heroku_Websocket_Sinatra_Padrino - Fatal编程技术网

Heroku Padrino websockets+;希罗库;连接在收到握手响应之前关闭

Heroku Padrino websockets+;希罗库;连接在收到握手响应之前关闭,heroku,websocket,sinatra,padrino,Heroku,Websocket,Sinatra,Padrino,我正在使用padrinowebsockets()为我的站点提供一个聊天系统,它在我的本地机器上运行得非常好。但是,在部署到heroku(免费)后,websocket将不会建立连接并返回 failed: Connection closed before receiving a handshake response 它在localhost上运行良好,我使用它来连接: connection = new WebSocket('ws://localhost:3000/channel'); 但是,当在h

我正在使用padrinowebsockets()为我的站点提供一个聊天系统,它在我的本地机器上运行得非常好。但是,在部署到heroku(免费)后,websocket将不会建立连接并返回

failed: Connection closed before receiving a handshake response
它在localhost上运行良好,我使用它来连接:

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中,这在我的proc文件中。发生了什么事

web: bundle exec puma -t 1:16 -p ${PORT:-3000} -e ${RACK_ENV:-production}
您的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');
这一行:

 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层共享同一个服务器,那么它应该可以工作