Socket.io access-control-allow=来自远程站点的起源错误

发布于 2024-11-24 19:32:06 字数 738 浏览 3 评论 0原文

我正在尝试从另一个站点访问 socket.io 服务器。它工作了几周,但现在我不断收到以下错误。从heroku上的服务器访问nodester上的服务器时会发生这种情况。错误是:

XMLHttpRequest cannot load http://site2.nodester.com/socket.io/xhr-polling//1311008802545. 
Origin http://site1.heroku.com is not allowed by Access-Control-Allow-Origin.
Resource interpreted as Script but transferred with MIME type text/plain.
Uncaught SyntaxError: Unexpected identifier

这是我与套接字连接的方式:

socket = new io.Socket(
    'site2.nodester.com', {port: 80, rememberTransport: false}
);

这是服务器代码:

// requires

server = http.createServer(function(req, res){
    // server stuffs
}),

server.listen(8362);

var io = io.listen(server),

// io code

I am trying to access a socket.io server from another site. It worked for a few weeks but now I keep getting the following error. It happens when accessing a server on nodester from a server on heroku. The error is:

XMLHttpRequest cannot load http://site2.nodester.com/socket.io/xhr-polling//1311008802545. 
Origin http://site1.heroku.com is not allowed by Access-Control-Allow-Origin.
Resource interpreted as Script but transferred with MIME type text/plain.
Uncaught SyntaxError: Unexpected identifier

Here's how I'm connecting with the socket:

socket = new io.Socket(
    'site2.nodester.com', {port: 80, rememberTransport: false}
);

And here's the server code:

// requires

server = http.createServer(function(req, res){
    // server stuffs
}),

server.listen(8362);

var io = io.listen(server),

// io code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

单身情人 2024-12-01 19:32:07

一个简单的解决方法(感谢 mattcodez):

io = io.listen(8888);
io.server.removeListener('request', io.server.listeners('request')[0]);

此问题有一个未解决的错误,您可以阅读有关它的更多信息 此处

A simple workaround (thanks to mattcodez):

io = io.listen(8888);
io.server.removeListener('request', io.server.listeners('request')[0]);

There is an open bug for this issue and you can read more about it here:

南薇 2024-12-01 19:32:07

我通过更改一行客户端代码解决了这个问题。

var socket = io('localhost');

改为

var socket = io();

I solved this by changing one line of client code.

I changed

var socket = io('localhost');

to

var socket = io();
断桥再见 2024-12-01 19:32:06

您是否配置 socket.io 仍使用默认origins = * 或至少输入 origins = site1.heroku.com

Did you configure socket.io to still use default origins = * or at least put origins = site1.heroku.com

落墨 2024-12-01 19:32:06

我面临的问题是,从不同的位置为客户端 socket.io.js 提供服务。

您可以通过从您尝试连接的同一服务器提供客户端 js 文件来避免此问题。

例如,我最初的客户端代码是这样的,

<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

一旦我将其修改为这样,它就会抛出错误,它工作正常。

<script src="http://mydomain.com/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

我的服务器代码是,

var express = require('express');
var app = express();
app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Headers", "Content-Type");
        res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
        next();
    });
var server = http.createServer(app);
io = socketio.listen(server, {log:false, origins:'*:*'});
... //io.connect and events below

The problem I faced was, serving the client socket.io.js from a different location.

You can avoid this issue by serving the client js file from the same server where you are trying to connect to.

for example, my initial client code was this and it was throwing error

<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

once I modified it to this, it worked alright.

<script src="http://mydomain.com/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

And my server code is,

var express = require('express');
var app = express();
app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Headers", "Content-Type");
        res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
        next();
    });
var server = http.createServer(app);
io = socketio.listen(server, {log:false, origins:'*:*'});
... //io.connect and events below
半仙 2024-12-01 19:32:06

我有同样的问题,但没有得到解决。

我测试了很多配置:

  io.set("origins","*");
  io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);

或者

  io.set("origins = *");
  io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);

或者

var socket  = io.listen(appS1,{origins: '*:*'});

谢谢你的帮助:)

I've a same problem and I don't arrive at resolved.

I test many configuration:

  io.set("origins","*");
  io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);

or

  io.set("origins = *");
  io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);

or

var socket  = io.listen(appS1,{origins: '*:*'});

thanks your help :)

囍笑 2024-12-01 19:32:06

它可能不完全相关,但最终可能会在这个问题上帮助一些可怜的人:

我刚刚遇到从本地磁盘运行客户端(至少在 Windows 上),例如。 file:///c:/... 导致“null”源,这将使源检查崩溃。尝试将客户端上传到远程网站并从那里启动它。将原点保留在 : 上。

希望这可以帮助一些人。

It might be not exactly related, but could eventually help some poor souls on this issue:

I've just encountered that running the client from local disk (at least on Windows) eg. file:///c:/... results in a "null" origin, which will crash the origins check. Try uploading the client to a remote website and fire it from there. Leave the origins on :.

Hope this helps some people out.

卖梦商人 2024-12-01 19:32:06

这对我有用

var app = require('express')()
  , server = require('http').createServer(app)
  , sio = require('socket.io');     

var io = sio.listen(server, { origins: '*:*' });

This worked for me

var app = require('express')()
  , server = require('http').createServer(app)
  , sio = require('socket.io');     

var io = sio.listen(server, { origins: '*:*' });

我通过从服务器本身加载客户端的 socket.io.js 解决了这个问题,而不是在站点上维护本地副本。在我的设置中,某个网站上有一个网页可以从位于不同位置的服务器获取文件。

I resolved the issue by loading the client's socket.io.js from the server itself, rather than maintaining a local copy on the site. In my setup, there is a webpage on a certain website that fetches the file from the server, which is at a different location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文