javascript - Pusher 未在 Heroku Rails 应用程序上触发,但在 localhost :3000 上工作正常

标签 javascript jquery ruby-on-rails heroku pusher

我正在 Ruby on Rails 应用程序上学习 Pusher,并将其设置为用户单击按钮,提交表单以更新对象,然后触发 Pusher 事件,发送警报并更改所有监听窗口的背景颜色在该 channel 上。

我的网站在本地服务器 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's help page ,我已经安装了 Pusher gem 和插件。

这是在我的 switches#update 中运行的触发器:

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

在 application.html.erb 上运行的监听器/绑定(bind):

<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>

我在同一文件的 header 中有这两个脚本:

<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 添加这些,效果很好。 See comment

最佳答案

您在 Heroku 日志中看到任何错误或消息吗?

此外,您是否在 Heroku 上尝试过一个简单的测试用例,除了通过 Pusher gem 发送消息之外什么也不做?

关于javascript - Pusher 未在 Heroku Rails 应用程序上触发,但在 localhost :3000 上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23483960/

相关文章:

javascript - 如何使用javascript在链接href中插入变量值?

ruby-on-rails - 来自 Sidekiq worker : How to pass a partial? 的邪恶 PDF 生成

html - 居中导航栏图标文本 - 使用 Bootstrap 的 Rails 应用程序

javascript - 检测标签显示,移除类隐藏

javascript - Adobe Edge Animate 和 d3.js : d3. scale.range([value]) 在 Chrome 和 Safari 中无法正常工作

在 JupyterLab 中禁用 JavaScript 输出

java - 读取一个jsp传递给另一个jsp的参数

jquery - 如何为这些嵌套的 div 元素设置动画大小? (jQuery)

ruby-on-rails - 如果在没有 www 前缀的情况下访问域,Facebook 登录将返回错误消息

javascript - Isotope JS 选项卡在第一次加载时显示所有内容