Socket.io + PhoneGap [英] Socket.io + PhoneGap

查看:181
本文介绍了Socket.io + PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Socket.io与PhoneGap我得到这个错误:



(在iOS应该支持socket.io)

  Access-Control-Allow-Origin不允许原始null。 

这是因为我的应用程式是透过 file:// 协议。

解决方案

p>您必须将socketio主机添加到PhoneGap.plist中的ExternalHosts键。



请参阅 Faq


外部主机的链接和导入的文件不会加载?



A。最新的代码具有新的白名单功能。如果您引用外部主机
,则必须在PhoneGap.plist中的ExternalHosts键下添加主机。通配符确定。
因此,如果您要连接到 http://phonegap.com ,则必须将phonegap.com添加到列表中(或使用通配符* .phonegap.com
,它也将匹配子域)。 (注意:如果在Xcode中打开plist
文件,则不需要使用XML语法。)


对于android你必须编辑cordova.xml并添加对socketio主机的访问:

 < access origin =HOST */> 






index.html(使用socketio示例):

  ... 
< script src =HOST / socket.io / socket.io.js> < / script>
< script>
var socket = io.connect('HOST');
socket.on('news',function(data){
socket.emit('my other event',{my:'data'});
});
< / script>
...






app.js (服务器端javascript /基本socketio示例):

  var io = require('socket.io' ; 

io.sockets.on('connection',function(socket){

socket.emit('news',{hello:'world'});
socket.on('my other event',function(data){
console.log(data);
});
});






您必须以主机名socket.io server!


When I try to use Socket.io with PhoneGap I get this error:

(on iOS where socket.io should be supported)

Origin null is not allowed by Access-Control-Allow-Origin.

This is because my app is served via file:// protocol. What can I do to get around this?

Thanks!!

解决方案

You have to add the socketio host to the "ExternalHosts" key in PhoneGap.plist.

See Faq:

Q. Links to and imported files from external hosts don't load?

A. The latest code has the new white-list feature. If you are referencing external hosts, you will have to add the host in PhoneGap.plist under the "ExternalHosts" key. Wildcards are ok. So if you are connecting to "http://phonegap.com", you have to add "phonegap.com" to the list (or use the wildcard "*.phonegap.com" which will match subdomains as well). (Note: If you open the plist file in Xcode, you won't need to fiddle with the XML syntax.)

For android you have to edit cordova.xml and add access to the socketio host:

<access origin="HOST*"/> 


index.html (with socketio example):

...
<script src="HOST/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('HOST');
    socket.on('news', function (data) {
        socket.emit('my other event', { my: 'data' });
    });
</script>
...


app.js (server side javascript / basic socketio example):

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {

socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});


The HOST you have to replace with hostname of your socket.io server!

这篇关于Socket.io + PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆