Javascript *.JS.erb中的Rails 3.1-JS-Socket.io-emit未被执行,并阻止jQuery函数的执行

Javascript *.JS.erb中的Rails 3.1-JS-Socket.io-emit未被执行,并阻止jQuery函数的执行,javascript,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,socket.io,Javascript,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,Socket.io,我想在Rails项目中使用node.js来服务异步io。我不想使用juggernaut、faye或类似的东西,因为我需要与web套接字、服务器发送事件和spdy的干净连接,没有其他选择。我第一次尝试使用node.js是现在使用Socket.io,只是为了使用JavaScript将数据提供给node.js-module。但它根本不起作用 My application.js如下所示: // This is a manifest file that'll be compiled into includ

我想在Rails项目中使用node.js来服务异步io。我不想使用juggernaut、faye或类似的东西,因为我需要与web套接字、服务器发送事件和spdy的干净连接,没有其他选择。我第一次尝试使用node.js是现在使用Socket.io,只是为了使用JavaScript将数据提供给node.js-module。但它根本不起作用

My application.js如下所示:

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

window.socket = io.connect('http://localhost:8080/');
io要求正确加载到my application.html.erb中:

 <%= javascript_include_tag "application", "http://localhost:8080/socket.io/socket.io.js" %>
在另一个名为index.js.erb的视图中,我通过以下方式收听此事件:

def create
  @article = current_user.articles.build(params[:article])
  if @article.save
    msg = {:data => @article.to_json}
    @message = msg.to_json
  end
end
$(function() {
  window.socket.on('message', function (msg) {
    $("#articles").prepend(<%= escape_javascript render(Article.new.from_json(msg.data)) %>);
  });
});
$.getScript('http://localhost:8080/socket.io/socket.io.js', function(){
  window.socket = io.connect('http://localhost:8080/');
});
而且似乎工作正常,因为它告诉我如何正确地服务于请求的js文件:

info  - socket.io started
debug - served static /socket.io.js
但是它根本不起作用,尽管create.js.erb渲染时没有出现错误:

Rendered articles/create.js.erb (0.5ms)
Completed 200 OK in 268ms (Views: 70.0ms | ActiveRecord: 14.6ms)
即使用于重置表单的jQuery函数也没有执行,这在没有try-to-emit套接字事件的情况下是有效的。文章被正确地保存到数据库中,所以这不是问题所在。有人能告诉我问题出在哪里吗

更新:通过使用chrome的JavaScript调试器,我发现问题始于application.js中window.socket的定义。错误代码为:

Uncaught ReferenceError: io is not defined
  (anonymous function)
但是io是在我加载到application.html.erb中的socket.io.js文件中定义的。我通过以下方式解决了application.js的问题:

def create
  @article = current_user.articles.build(params[:article])
  if @article.save
    msg = {:data => @article.to_json}
    @message = msg.to_json
  end
end
$(function() {
  window.socket.on('message', function (msg) {
    $("#articles").prepend(<%= escape_javascript render(Article.new.from_json(msg.data)) %>);
  });
});
$.getScript('http://localhost:8080/socket.io/socket.io.js', function(){
  window.socket = io.connect('http://localhost:8080/');
});

现在的问题是:尽管application.html.erb中的脚本是loadet,但为什么我必须以这种方式获取脚本?但是,尽管有这种解决方法,create.js.erb仍然不起作用。我会在睡一觉后尝试解决这个问题。

我通常会在相关清单中包含依赖项(在本例中,
application.js

Rails知道许多不同的资产路径,所有这些路径都是在编译之前检查的。我倾向于将(例如,
socket.io.js
)放在
vendor/assets/javascripts
中,并在清单中添加一行额外内容,如下所示:

//= require jquery
//= require jquery_ujs
//= require socket.io
//= require_tree .