浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

解决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 接收不了数据。。。。有人知道是什么原因,麻烦你告诉我。。好了谢谢...

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

posted on 2012-05-10 07:37  lexus  阅读(2458)  评论(0编辑  收藏  举报