Rails没有'Access-Control-Allow-Origin'头问题与未处理的socket.io url

时间:2016-08-25 11:48:30

标签: ruby-on-rails node.js socket.io cors webrtc

我正在测试一个关于rails的webrtc演示,但是我遇到了很糟糕的连接问题。我在网上尝试了很多东西但是无法解决这个问题,这可能是一个简单的错误或基本的误解,但我所做的就是:

使用NodeJS和socket.io的webrtc-rails

我使用localhost:3000作为页面的主页,但我尝试通过localhost:2013从客户端连接到服务器。我目前有两条错误消息:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

info  - unhandled socket.io url   (when  running node server.js)

我的设置是:

server.js

var http = require('http');
var app = http.createServer(function (req, res) {
}).listen(2013);

client.js

var socket = io.connect('localhost:2013');

socket.on("created", function (){
  console.log("On Created");
  isInitiator = true;
  console.log('isInitiator', isInitiator);
})

application.rb中

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

config.ru

require 'rack/cors'

use Rack::Cors do

  allow do
    origins '*'
    resource '*',
        :headers => :any,
        :methods => [:get, :post, :delete, :put, :options, :patch]
  end

end

我更新了所有宝石和socket.io。我一直想知道找到socket.io是否有问题,好像我导航到页面localhost:3000 / socket.io / socket.io.js,没有任何东西像我期望的那样......我也试过添加CORS没有中间件/机架宝石的应用程序rb rile的标题,但它没有任何区别。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要将“http” - 或任何协议 - 附加到您尝试连接的网址。

其次,而不是引用localhost引用127.0.0.1。仍然像平常一样参考端口号。

var socket = io.connect('localhost:2013');

会变成:

var socket = io.connect('http://127.0.0.1:2013');

编辑:只有在出于某种原因为您的服务器分配了不同的IP地址时,这才有效。话虽如此,您仍然希望将协议添加到您的网址。