Rails 3.1 - JS - * .js.erb中的Socket.io-emit未执行并阻止执行jQuery-Function [英] Rails 3.1 - JS - Socket.io-emit in *.js.erb gets not executed and prevents execution of jQuery-Function

查看:95
本文介绍了Rails 3.1 - JS - * .js.erb中的Socket.io-emit未执行并阻止执行jQuery-Function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use node.js with my Rails-Project to serve asynchronous io. I don't want to use juggernaut, faye or something like this, because I need clean connections with web-socket, server-sent events and spdy without an alternative. My first try to use node.js is now to use Socket.io, just to get in use with serving data to a node.js-module with JavaScript. But it doesn't work at all.

我的application.js看起来像这样:

My application.js looks like this:

// 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的要求在我的application.html.erb中正确加载:

The requirements for io get correctly loaded in my application.html.erb:

 <%= javascript_include_tag "application", "http://localhost:8080/socket.io/socket.io.js" %>

现在我想使用套接字发送一个事件发送给所有的侦听客户端.js.erb:

Now I want to use the socket to emit a event sent to all listening clients like this in a create.js.erb:

$(function() {
  window.socket.emit('message', <%= @message =>);
};
$("#new_article")[0].reset();

消息以这种方式在ArticlesCrontroller中创建:

The message gets created in the ArticlesCrontroller this way:

def create
  @article = current_user.articles.build(params[:article])
  if @article.save
    msg = {:data => @article.to_json}
    @message = msg.to_json
  end
end

我在这个事件中听另一种名为index.js.erb的视图:

I listen at this event in an other view called index.js.erb this way:

$(function() {
  window.socket.on('message', function (msg) {
    $("#articles").prepend(<%= escape_javascript render(Article.new.from_json(msg.data)) %>);
  });
});

Socket.io如下所示:

The Socket.io looks like this:

var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
  socket.on('message', function () { });
  socket.on('disconnect', function () { });
});

似乎工作正常,因为它告诉我它如何服务于请求的js-File:

and seems to work right, because it tells me right how it serves the requested js-File:

info  - socket.io started
debug - served static /socket.io.js

但它根本不起作用,尽管create.js.erb在没有错误的情况下呈现:

But it doesn't work at all, although the create.js.erb gets rendered without an error:

Rendered articles/create.js.erb (0.5ms)
Completed 200 OK in 268ms (Views: 70.0ms | ActiveRecord: 14.6ms)

即使用于重置表单的jQuery-Function也没有被执行,没有尝试发出的有效插座事件。文章正确保存到数据库,所以这不是问题。有人可以告诉我问题可能在哪里吗?

Even the jQuery-Function for resetting the form gets not executed, what works without the try to emit the socket-event. The article gets saved to the database correctly, so that isn't the problem. Can someone tell me where the problem might be?

更新我通过使用Chrome的JavaScript调试器来解决问题在application.js中定义 window.socket 。错误代码是:

Update: I figured out by using the JavaScript-debugger of chrome, that the problem starts with the definition of window.socket in the application.js. The error-code is:

Uncaught ReferenceError: io is not defined
  (anonymous function)

io 是在我的应用程序中加载的socket.io.js文件中定义的。 html.erb。我以这种方式为application.js做了一个解决方法:

But io is defined in the socket.io.js-File I load in my application.html.erb. I did a workaround for application.js this way:

$.getScript('http://localhost:8080/socket.io/socket.io.js', function(){
  window.socket = io.connect('http://localhost:8080/');
});

现在的问题是:为什么我必须以这种方式获取脚本,尽管它是loadet in application.html.erb?但是尽管有这种解决方法,create.js.erb仍然无法正常工作。我会尝试解决一下睡眠后的问题。

The question is now: Why do I have to get the script this way, although it is loadet in the application.html.erb? But despite this workaround the create.js.erb still doesn't work yet. I will try to workaround that after having a bit of sleep.

推荐答案

我通常在相关清单中包含依赖项(在这种情况下) , application.js )。

I generally include dependencies in the relevant manifest (in this case, application.js).

Rails了解许多不同的资产路径,所有资产路径都在编译之前进行了检查。我倾向于在 vendor / assets / javascripts 中添加(例如 socket.io.js )并添加一个清单的额外行如下:

Rails has awareness of many different asset paths, all of which are checked prior to compilation. I'd be inclined to put (e.g. socket.io.js) in vendor/assets/javascripts and add an extra line to the manifest as follows:

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

这篇关于Rails 3.1 - JS - * .js.erb中的Socket.io-emit未执行并阻止执行jQuery-Function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆