解决nodejs socket.io is not allowed by Access-Control-Allow-Origin 跨域问题
发布于 3年前 作者 solqkwgkwg 6544 次浏览

解决nodejs socket.io is not allowed by Access-Control-Allow-Origin 跨域问题 blog:http://www.cnblogs.com/solq/ demo:http://unitysgui.sinaapp.com/websocket/socketio.html

更改 :\node_modules\socket.io\lib\manager.js

if (origin) { // https://developer.mozilla.org/En/HTTP_Access_Control headers[‘Access-Control-Allow-Origin’] = origin; headers[‘Access-Control-Allow-Credentials’] = 'true’; //headers[‘Access-Control-Allow-Headers’] = 'Referer’; } headers[‘Access-Control-Allow-Origin’] = "*";

在后面添加 headers[‘Access-Control-Allow-Origin’] = "*";

server.js

var http = require(‘http’),
io = require(‘socket.io’), sys = require(‘sys’);

server = http.createServer(function(req, res){ res.writeHead(200, {’Content-Type’: 'text/html’}); res.end(“hello”); }); server.listen(8082);

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

/* socket.set(“origins",”"); socket.set('transports’, [ ‘websocket’ , ‘flashsocket’ , ‘htmlfile’ , ‘xhr-polling’ , ‘jsonp-polling’ ]);/

socket.on('connection’, function(client){

sys.puts("New client is here!");
client.send("hello world");

client.on('message', function(msg){ sys.puts("client has sent:"+msg); }) ;
client.on('disconnect', function(){ sys.puts("Client has disconnected"); }) ;

//发送自定义事件
client.emit('news', { hello: 'news world' });

//临听自定义事件
client.on('my other event', function (data) {
    //console.log(data);
});

});

client.js

<script src="http://localhost:8082/socket.io/socket.io.js"></script> <script> window.onload=function(){
var url='127.0.0.1:8082';
var socket = io.connect('localhost',{port:8082,rememberTransport:true,timeout:1500});
//var socket = new io.Socket(null,{port:8082,rememberTransport:true,timeout:1500});

/*
    if (/Firefox\/\s/.test(navigator.userAgent)){
    var socket = io.connect(url,{transports:['xhr-polling']}); 
} 
else if (/MSIE (\d+.\d+);/.test(navigator.userAgent)){
    var socket = io.connect(url,{transports:['jsonp-polling']}); 
} 
else { 
    var socket = io.connect(url,{transports:['websocket']}); 
}
*/

//socket.connect();
socket.on('connect', function(){ 
    console.log('connected to server++++++++++++++++'); 
    socket.send('Hi Server...'); 
}) ;
socket.on('message', function(r){ console.log('msg:+++++++++++'+r); }) ;
socket.on('disconnect', function(){ console.log('disconnected from server'); }) ;

socket.on('news', function (data) {
    console.log("++++++++++++++++++++++++++");
    console.log(data);
    
    //发送自定义事件
    socket.emit('my other event', { my: 'data' });
});

/*XMLHttpRequest cannot load http://localhost:8082/socket.io/1/?t=1336306289263. Origin null is not allowed by Access-Control-Allow-Origin.
var ws = new WebSocket("ws://127.0.0.1:8082");
ws.onopen = function(){console.log('connected to server');}
ws.onmessage = function(m){console.log('onmessage');}
ws.onclose = function(){}*/

}

</script>

https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO 注意:经过测试 io.connect(‘localhost’) localhost:8082 加上端会连不上

经过测试。。

if (origin!=’null’) { // https://developer.mozilla.org/En/HTTP_Access_Control headers[‘Access-Control-Allow-Origin’] = origin; headers[‘Access-Control-Allow-Credentials’] = 'true’; //headers[‘Access-Control-Allow-Headers’] = 'Referer’; } console.log(“console.log(origin)+++++++++++++++++++++++++++++++++++++++”) console.log(origin) console.log(req.headers)

origin==null…难怪连不上。。。为什么为是null不太懂原理。。。就不管了,,手动改为全部可以访问就行了

解决IE不能接收服务端信息问题: sever 要打开这几个协议吧。。。。 然后 clinet html 要放在服务里面,因为IE用的是 jsonp 方式 的话。。。。。。。。。。。只要是 js 请求都要放在服务器… 但是放在服务器里, node server 接收 ie 客户端 连接类型为 flashsocket 方式,反正是不太了解。。。能工作就行了。。哈哈

socket.set('transports’, [ ‘websocket’ , ‘flashsocket’ , ‘htmlfile’ , ‘xhr-polling’ , ‘jsonp-polling’ ]);

最后,经过反复测试,,以上打开的协议是 遍历检测有就使用的。。。。 如果把 flashsocket 放在最后,那么 server 跟 ie 就用 jsonp 方式来连接…但是 ie 接收不了数据。。。。有人知道是什么原因,麻烦你告诉我。。好了谢谢…

10 回复

汗,,,怎么没得排版的。。。。。。。。。。。。。。。。

需要学习以下markdown语法

解决nodejs socket.io is not allowed by Access-Control-Allow-Origin 跨域问题

blog:http://www.cnblogs.com/solq/
demo:http://unitysgui.sinaapp.com/websocket/socketio.html

更改 :\node_modules\socket.io\lib\manager.js

if (origin) {
    // https://developer.mozilla.org/En/HTTP_Access_Control
    headers['Access-Control-Allow-Origin'] = origin;
    headers['Access-Control-Allow-Credentials'] = 'true';
    //headers['Access-Control-Allow-Headers'] = 'Referer';
  }
    headers['Access-Control-Allow-Origin'] = "*";

在后面添加 headers[‘Access-Control-Allow-Origin’] = "*";

server.js

var http = require('http'),  
io = require('socket.io'),
sys = require('sys');

server = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end("hello");
});
server.listen(8082);

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

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


socket.on('connection', function(client){ 

    sys.puts("New client is here!");
    client.send("hello world");

    client.on('message', function(msg){ sys.puts("client has sent:"+msg); }) ;
    client.on('disconnect', function(){ sys.puts("Client has disconnected"); }) ;
    
    //发送自定义事件
    client.emit('news', { hello: 'news world' });

    //临听自定义事件
    client.on('my other event', function (data) {
        //console.log(data);
    });
}); 

client.js

<script src="http://localhost:8082/socket.io/socket.io.js"></script>
<script>
window.onload=function(){
    
    var url='127.0.0.1:8082';
    var socket = io.connect('localhost',{port:8082,rememberTransport:true,timeout:1500});
 
    socket.on('connect', function(){ 
        console.log('connected to server++++++++++++++++'); 
        socket.send('Hi Server...'); 
    }) ;
    socket.on('message', function(r){ console.log('msg:+++++++++++'+r); }) ;
    socket.on('disconnect', function(){ console.log('disconnected from server'); }) ;

    socket.on('news', function (data) {
        console.log("++++++++++++++++++++++++++");
        console.log(data);
        
        //发送自定义事件
        socket.emit('my other event', { my: 'data' });
    });
    
    /*XMLHttpRequest cannot load http://localhost:8082/socket.io/1/?t=1336306289263. Origin null is not allowed by Access-Control-Allow-Origin.
    var ws = new WebSocket("ws://127.0.0.1:8082");
    ws.onopen = function(){console.log('connected to server');}
    ws.onmessage = function(m){console.log('onmessage');}
    ws.onclose = function(){}*/
}
</script>

https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO 注意:经过测试 io.connect(‘localhost’) localhost:8082 加上端会连不上

 if (origin!='null') {
    // https://developer.mozilla.org/En/HTTP_Access_Control
    headers['Access-Control-Allow-Origin'] = origin;
    headers['Access-Control-Allow-Credentials'] = 'true';
    //headers['Access-Control-Allow-Headers'] = 'Referer';
  }
    console.log("console.log(origin)+++++++++++++++++++++++++++++++++++++++")
    console.log(origin)
    console.log(req.headers)

origin==null…难怪连不上。。。为什么为是null不太懂原理。。。就不管了,,手动改为全部可以访问就行了

解决IE不能接收服务端信息问题: sever 要打开这几个协议吧。。。。 然后 clinet html 要放在服务里面,因为IE用的是 jsonp 方式 的话。。。。。。。。。。。只要是 js 请求都要放在服务器… 但是放在服务器里, node server 接收 ie 客户端 连接类型为 flashsocket 方式,反正是不太了解。。。能工作就行了。。哈哈

socket.set('transports', [
    'websocket'
    , 'flashsocket'
    , 'htmlfile'
    , 'xhr-polling'
    , 'jsonp-polling'
]);

最后,经过反复测试,,以上打开的协议是 遍历检测有就使用的。。。。 如果把 flashsocket 放在最后,那么 server 跟 ie 就用 jsonp 方式来连接…但是 ie 接收不了数据。。。。有人知道是什么原因,麻烦你告诉我。。好了谢谢…

希望能给个邀请码。。。。。

… 楼上是发布者的话, 帖子描述的地方有个图标可以重新编辑帖子的…
而且全角句号连用看得人太纠结…

前几天我遇到这个问题搜到解决方案是这个(Chrome 20 上测试了…) http://stackoverflow.com/questions/6736706/socket-io-access-control-allow-origin-error-from-remote-site

io = require('socket.io').listen(app, {origins: '*:*'});

看楼主代码上这个还是注释了的(*个数不一样), 两个方案区别怎么样的?

如何用火孤或者google浏览器的话, clinet html 可以不用放在服务器里。。因为用的是 websocket连接方式。。。

我加了 {origins: ':'} 还是不行,所有就去掉了

这个看懂了, 好吧原来我看漏掉了… 除了注释那里意外还是有指定 origins 的…
IE 我没在用, 刚好事情都省了…

1、{origins: '*:*'}这句其实是没必要的,因为这是manager.js里面的默认值

2、headers['Access-Control-Allow-Origin'] = "*";这句也是没有必要的, 上面的headers['Access-Control-Allow-Origin'] = origin;已经足够了

至于跨域出现的问题首先要确保你的测试页面是放在服务器上的,如果是放在本机上的服务器,请求地址用127.0.0.1:xxxx 代替 localhost:xxxx,因为在跨域的时候,IE8以上优先用的不是jsonp,而是xhr-polling(XDomainRequest),因为XDomainRequest跨域请求localhost会拒绝访问

回到顶部