gpt4 book ai didi

heroku - Padrino websockets + Heroku;连接在收到握手响应之前关闭

转载 作者:行者123 更新时间:2023-12-02 09:25:20 25 4
gpt4 key购买 nike

我正在使用 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}

最佳答案

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

关于heroku - Padrino websockets + Heroku;连接在收到握手响应之前关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32709270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com