zoukankan      html  css  js  c++  java
  • 解决nodejs socket.io is not allowed by AccessControlAllowOrigin 跨域问题 CNode

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

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

  • 相关阅读:
    《Node.js 包教不包会》
    (转)Java并发编程:线程池的使用方法
    (原创)定时线程池中scheduleWithFixedDelay和scheduleAtFixedRate的区别
    JAVA线程本地变量ThreadLocal和私有变量的区别
    (原创)确保JAVA线程安全的4种常用方法
    (转)ReentrantLock可重入锁的使用场景
    (原创)JAVA阻塞队列LinkedBlockingQueue 以及非阻塞队列ConcurrentLinkedQueue 的区别
    读/写锁的实现和应用(高并发状态下的map实现)
    Jira API传字符串的换行问题 (文本编辑器使用)
    使用泛型SwingWorker与EDT事件分发线程保持通讯
  • 原文地址:https://www.cnblogs.com/lexus/p/2493651.html
Copyright © 2011-2022 走看看