簡體   English   中英

Pusher 未在 Heroku Rails 應用程序上觸發,但在 localhost:3000 上工作正常

[英]Pusher not triggering on Heroku Rails app, but works fine on localhost:3000

我在 Ruby on Rails 應用程序上學習 Pusher,並在用戶單擊按鈕的位置進行設置,它提交表單以更新對象,然后觸發 Pusher 事件發送警報並更改在該通道上偵聽的所有窗口的背景顏色.

我的站點在我的本地服務器 localhost:3000 和 Heroku 上運行。 在 localhost:3000 上,這就像一個魅力 - 當我打開在本地和 Heroku 上運行該站點的窗口時,我單擊本地站點上的按鈕,所有窗口都會收到警報並更新背景顏色。

但是,當我在 Heroku 上嘗試相同的操作時,沒有一個窗口會收到警報或更改顏色。 我查看了 Pusher 上的調試控制台,它顯示已建立連接,但未顯示發送的 API 請求:

Subscribed    35329.28868505   Channel:  client-open    23:52:33
Connection    35329.28868505   Origin:   http://testing-pusher.herokuapp.com    23:52:33
Disconnection  35303.28886132  Channels: client-open, Lifetime: 107.70291038s   23:52:32

根據Heroku 的幫助頁面,我已經安裝了 pusher gem 和插件。

這是在我的switches#update中運行的觸發器:

@switch.update(color: params[:switch][:color])
Pusher['client-open'].trigger('new_message', {:color => @switch.color})

在 application.html.erb 上運行的偵聽器/綁定:

<script type="text/javascript" charset="utf-8">
$(function() {
    Pusher.log = function(message) {
        if (window.console && window.console.log) {
            window.console.log(message);
        }
    };

    var pusher = new Pusher('my_pusher_key'); // This has my actual key in it
    var channel = pusher.subscribe('client-open');
    channel.bind('new_message', function(data) {
        alert("I'm going to change your background color to #"+data.color);
        $("#appended").append("I'm going to change your background color to "+data.color);
        $(".color").css('background-color', '#'+data.color);
});

// Some useful Pusher debug msgs
pusher.connection.bind('connecting', function() {
    $('div#status').text('Connecting to Pusher...');
});

pusher.connection.bind('connected', function() {
    $('div#status').text('Connected to Pusher!');
});
    pusher.connection.bind('failed', function() {
      $('div#status').text('Connection to Pusher failed :(');
    });
    channel.bind('subscription_error', function(status) {
      $('div#status').text('Pusher subscription_error');
    });
  });
</script>

我在同一個文件的標題中有這兩個腳本:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://js.pusherapp.com/2.0/pusher.min.js"></script>

如何讓 Pusher 在 Heroku 上正確觸發?

編輯:這是 Heroku 日志。 我可以說沒有錯誤:

2014-05-07T18:40:46.291870+00:00 heroku[router]: at=info method=POST path=/switches/1 host=testing-pusher.herokuapp.com request_id=f3f9e17c-086b-4639-88ba-4a406828c4e7 fwd="24.20.217.80" dyno=web.1 connect=2ms service=72ms status=302 bytes=942
2014-05-07T18:40:46.224270+00:00 app[web.1]: Started PATCH "/switches/1" for 24.20.217.80 at 2014-05-07 18:40:46 +0000
2014-05-07T18:40:46.224278+00:00 app[web.1]: Started PATCH "/switches/1" for 24.20.217.80 at 2014-05-07 18:40:46 +0000
2014-05-07T18:40:46.227649+00:00 app[web.1]: Processing by SwitchesController#update as HTML
2014-05-07T18:40:46.415613+00:00 app[web.1]: Started GET "/" for 24.20.217.80 at 2014-05-07 18:40:46 +0000
2014-05-07T18:40:46.455745+00:00 app[web.1]: Completed 200 OK in 28ms (Views: 19.0ms | ActiveRecord: 1.4ms)
2014-05-07T18:40:46.230175+00:00 app[web.1]: Processing by SwitchesController#update as HTML
2014-05-07T18:40:46.424400+00:00 app[web.1]: Processing by SwitchesController#show as HTML
2014-05-07T18:40:46.451080+00:00 app[web.1]:   Rendered switches/_form.html.haml (7.4ms)
2014-05-07T18:40:46.230262+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"nqhkV37GG6ym+pM7j+Bdxh3aBltTLfNpPa6cnE6b92U=", "switch"=>{"color"=>"c4b6c2"}, "commit"=>"push me", "id"=>"1"}
2014-05-07T18:40:46.230298+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"nqhkV37GG6ym+pM7j+Bdxh3aBltTLfNpPa6cnE6b92U=", "switch"=>{"color"=>"c4b6c2"}, "commit"=>"push me", "id"=>"1"}
2014-05-07T18:40:46.287569+00:00 app[web.1]: Redirected to http://testing-pusher.herokuapp.com/
2014-05-07T18:40:46.287617+00:00 app[web.1]: Redirected to http://testing-pusher.herokuapp.com/
2014-05-07T18:40:46.287866+00:00 app[web.1]: Completed 302 Found in 57ms (ActiveRecord: 8.2ms)
2014-05-07T18:40:46.287875+00:00 app[web.1]: Completed 302 Found in 57ms (ActiveRecord: 8.2ms)
2014-05-07T18:40:46.409915+00:00 app[web.1]: Started GET "/" for 24.20.217.80 at 2014-05-07 18:40:46 +0000
2014-05-07T18:40:46.423250+00:00 app[web.1]: Processing by SwitchesController#show as HTML
2014-05-07T18:40:46.451349+00:00 app[web.1]:   Rendered switches/show.html.haml within layouts/application (12.7ms)
2014-05-07T18:40:46.446554+00:00 app[web.1]:   Rendered switches/_form.html.haml (7.4ms)
2014-05-07T18:40:46.452492+00:00 app[web.1]: Completed 200 OK in 28ms (Views: 19.0ms | ActiveRecord: 1.4ms)
2014-05-07T18:40:46.451312+00:00 app[web.1]:   Rendered switches/show.html.haml within layouts/application (12.7ms)

編輯 2:問題只是缺少 heroku 上的 Pusher 憑據。 通過 ENV 添加了這些,它工作正常。 見評論

您是否在 Heroku 日志中看到任何錯誤或消息?

此外,您是否在 Heroku 上嘗試過一個簡單的測試用例,除了通過 Pusher gem 發送消息之外什么都不做?

我認為您需要更改 Pusher 應用程序憑據。

創建推送應用程序時,您可以選擇代表本地區域集群的集群。 例如,“ap3”表示亞太地區(東京),“eu”表示“歐洲”。

所以你最好在部署到服務器時更改你的 Pusher 應用程序。

演出遲到了,但如果其他人遇到這個......對我來說,我必須從我的配置中刪除這些

const pusher = new Pusher({
  appId: 'APP_ID',
  key: 'APP_KEY',
  secret: process.env.PUSHER_SECRET,
  cluster: 'us2',
  // these two were causing pusher.trigger to not work on deployed server
  // useTLS: true
  // encrypted: true
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM