解决nodejs socket.io is not allowed by Access-Control-Allow-Origin 跨域问题
[P][P]解决nodejs socket.io is not allowed by Access-Control-Allow-Origin 跨域问题[/P][P]blog:[URL=http://www.cnblogs.com/solq/]http://www.cnblogs.com/solq/[/URL][/P][P]demo:[URL=http://unitysgui.sinaapp.com/websocket/socketio.html]http://unitysgui.sinaapp.com/websocket/socketio.html[/URL][/P][P] [/P][P]更改 :\node_modules\socket.io\lib\manager.js[/P][P][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][COLOR=rgb(0, 0, 255)]if[/COLOR][COLOR=rgb(0, 0, 0)] (origin) { [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)] https://developer.mozilla.org/En/HTTP_Access_Control[/COLOR] headers['Access-Control-Allow-Origin'] =[COLOR=rgb(0, 0, 0)] origin; headers[[/COLOR]'Access-Control-Allow-Credentials'] = 'true'[COLOR=rgb(0, 0, 0)]; [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]headers['Access-Control-Allow-Headers'] = 'Referer';[/COLOR][COLOR=rgb(0, 0, 0)] } headers[[/COLOR]'Access-Control-Allow-Origin'] = "*";[P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][/P][P]在后面添加[/P]headers['Access-Control-Allow-Origin'] = "*";
server.js[P][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][COLOR=rgb(0, 0, 255)]var[/COLOR] http = require('http'[COLOR=rgb(0, 0, 0)]), io [/COLOR]= require('socket.io'[COLOR=rgb(0, 0, 0)]),sys [/COLOR]= require('sys'[COLOR=rgb(0, 0, 0)]);server [/COLOR]= http.createServer([COLOR=rgb(0, 0, 255)]function[/COLOR][COLOR=rgb(0, 0, 0)](req, res){ res.writeHead([/COLOR]200, {'Content-Type': 'text/html'[COLOR=rgb(0, 0, 0)]}); res.end([/COLOR]"hello"[COLOR=rgb(0, 0, 0)]);});server.listen([/COLOR]8082[COLOR=rgb(0, 0, 0)]);[/COLOR][COLOR=rgb(0, 0, 255)]var[/COLOR] socket = io.listen(server,{origins: '*:*'[COLOR=rgb(0, 0, 0)]}); [/COLOR][COLOR=rgb(0, 128, 0)]/*[/COLOR][COLOR=rgb(0, 128, 0)]socket.set("origins","*");socket.set('transports', [ 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling']);[/COLOR][COLOR=rgb(0, 128, 0)]*/[/COLOR][COLOR=rgb(0, 0, 0)]socket.on([/COLOR]'connection', [COLOR=rgb(0, 0, 255)]function[/COLOR][COLOR=rgb(0, 0, 0)](client){ sys.puts([/COLOR]"New client is here!"[COLOR=rgb(0, 0, 0)]); client.send([/COLOR]"hello world"[COLOR=rgb(0, 0, 0)]); client.on([/COLOR]'message', [COLOR=rgb(0, 0, 255)]function[/COLOR](msg){ sys.puts("client has sent:"+[COLOR=rgb(0, 0, 0)]msg); }) ; client.on([/COLOR]'disconnect', [COLOR=rgb(0, 0, 255)]function[/COLOR](){ sys.puts("Client has disconnected"[COLOR=rgb(0, 0, 0)]); }) ; [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]发送自定义事件[/COLOR] client.emit('news', { hello: 'news world'[COLOR=rgb(0, 0, 0)] }); [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]临听自定义事件[/COLOR] client.on('my other event', [COLOR=rgb(0, 0, 255)]function[/COLOR][COLOR=rgb(0, 0, 0)] (data) { [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]console.log(data);[/COLOR][COLOR=rgb(0, 0, 0)] });}); [/COLOR][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][/P][P]client.js[/P][P][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P]<script src="http://localhost:8082/socket.io/socket.io.js"></script><script>[COLOR=rgb(0, 0, 0)]window.onload[/COLOR]=[COLOR=rgb(0, 0, 255)]function[/COLOR][COLOR=rgb(0, 0, 0)](){ [/COLOR][COLOR=rgb(0, 0, 255)]var[/COLOR] url='127.0.0.1:8082'[COLOR=rgb(0, 0, 0)]; [/COLOR][COLOR=rgb(0, 0, 255)]var[/COLOR] socket = io.connect('localhost',{port:8082,rememberTransport:[COLOR=rgb(0, 0, 255)]true[/COLOR],timeout:1500[COLOR=rgb(0, 0, 0)]}); [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]var socket = new io.Socket(null,{port:8082,rememberTransport:true,timeout:1500});[/COLOR] [COLOR=rgb(0, 128, 0)]/*[/COLOR][COLOR=rgb(0, 128, 0)] 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']}); } [/COLOR][COLOR=rgb(0, 128, 0)]*/[/COLOR] [COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]socket.connect();[/COLOR] socket.on('connect', [COLOR=rgb(0, 0, 255)]function[/COLOR][COLOR=rgb(0, 0, 0)](){ console.log([/COLOR]'connected to server++++++++++++++++'[COLOR=rgb(0, 0, 0)]); socket.send([/COLOR]'Hi Server...'[COLOR=rgb(0, 0, 0)]); }) ; socket.on([/COLOR]'message', [COLOR=rgb(0, 0, 255)]function[/COLOR](r){ console.log('msg:+++++++++++'+[COLOR=rgb(0, 0, 0)]r); }) ; socket.on([/COLOR]'disconnect', [COLOR=rgb(0, 0, 255)]function[/COLOR](){ console.log('disconnected from server'[COLOR=rgb(0, 0, 0)]); }) ; socket.on([/COLOR]'news', [COLOR=rgb(0, 0, 255)]function[/COLOR][COLOR=rgb(0, 0, 0)] (data) { console.log([/COLOR]"++++++++++++++++++++++++++"[COLOR=rgb(0, 0, 0)]); console.log(data); [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]发送自定义事件[/COLOR] socket.emit('my other event', { my: 'data'[COLOR=rgb(0, 0, 0)] }); }); [/COLOR][COLOR=rgb(0, 128, 0)]/*[/COLOR][COLOR=rgb(0, 128, 0)]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(){}[/COLOR][COLOR=rgb(0, 128, 0)]*/[/COLOR][COLOR=rgb(0, 0, 0)]}[/COLOR]</script>[P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][/P][P][URL=https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO]https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO[/URL][/P][P]注意:经过测试[/P]io.connect('localhost')localhost:8082 加上端会连不上
经过测试。。[P][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P] [COLOR=rgb(0, 0, 255)]if[/COLOR] (origin!='null'[COLOR=rgb(0, 0, 0)]) { [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)] https://developer.mozilla.org/En/HTTP_Access_Control[/COLOR] headers['Access-Control-Allow-Origin'] =[COLOR=rgb(0, 0, 0)] origin; headers[[/COLOR]'Access-Control-Allow-Credentials'] = 'true'[COLOR=rgb(0, 0, 0)]; [/COLOR][COLOR=rgb(0, 128, 0)]//[/COLOR][COLOR=rgb(0, 128, 0)]headers['Access-Control-Allow-Headers'] = 'Referer';[/COLOR][COLOR=rgb(0, 0, 0)] } console.log([/COLOR]"console.log(origin)+++++++++++++++++++++++++++++++++++++++"[COLOR=rgb(0, 0, 0)]) console.log(origin) console.log(req.headers)[/COLOR][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][/P][P] [/P][P]origin==null...难怪连不上。。。为什么为是null不太懂原理。。。就不管了,,手动改为全部可以访问就行了[/P]
解决IE不能接收服务端信息问题:
sever 要打开这几个协议吧。。。。
然后 clinet html 要放在服务里面,因为IE用的是 jsonp 方式 的话。。。。。。。。。。。只要是 js 请求都要放在服务器..
但是放在服务器里, node server 接收 ie 客户端 连接类型为 flashsocket 方式,,反正是不太了解。。。能工作就行了。。哈哈[P][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P]socket.set('transports'[COLOR=rgb(0, 0, 0)], [ [/COLOR]'websocket'[COLOR=rgb(0, 0, 0)] , [/COLOR]'flashsocket'[COLOR=rgb(0, 0, 0)] , [/COLOR]'htmlfile'[COLOR=rgb(0, 0, 0)] , [/COLOR]'xhr-polling'[COLOR=rgb(0, 0, 0)] , [/COLOR]'jsonp-polling'[COLOR=rgb(0, 0, 0)]]);[/COLOR][P][URL=javascript:void(0);][IMG]http://common.cnblogs.com/images/copycode.gif[/IMG][/URL][/P][/P][P]最后,经过反复测试,,以上打开的协议是 遍历检测有就使用的。。。。[/P][P]如果把 flashsocket 放在最后,那么 server 跟 ie 就用 jsonp 方式来连接...但是 ie 接收不了数据。。。。有人知道是什么原因,麻烦你告诉我。。好了谢谢...[/P][P]
[/P][P]from: [URL=http://www.cnblogs.com/solq/archive/2012/05/06/2486540.html]http://www.cnblogs.com/solq/archive/2012/05/06/2486540.html[/URL][/P][/P]