Javascript Socket.io + PhoneGap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10738073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:43:56  来源:igfitidea点击:

Socket.io + PhoneGap

javascriptnode.jscordovasocket.io

提问by fancy

When I try to use Socket.IOwith PhoneGapI get this error:

当我尝试将Socket.IOPhoneGap一起使用,出现此错误:

(on iOS where socket.io should be supported)

(在应支持 socket.io 的 iOS 上)

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

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

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

这是因为我的应用程序是通过file://协议提供的。我能做些什么来解决这个问题?

Thanks!!

谢谢!!

回答by fancy

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

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

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.)

问:外部主机的链接和导入的文件无法加载?

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

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

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

<access origin="HOST*"/> 


index.html (with socketio example):

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 (server side javascript / basic socketio example):

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

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 服务器的主机名替换 HOST!

回答by BFil

Using PhoneGap the webpage is opened using the file://protocol

使用 PhoneGap 使用file://协议打开网页

With file://protocol no origin is set to the WebSocket connection, so the browser will raise that security exception if the server doesn't set the Access-Control-Allow-Originheader to the response enabling CORS

使用file://协议没有为 WebSocket 连接设置源,因此如果服务器没有将Access-Control-Allow-Origin标头设置为启用CORS的响应,浏览器将引发该安全异常

Consider using some PhoneGap plugin like the following, which uses native code to handle the connection, but enables a (hopefully standard-compatible) WebSocket API inside the webviews

考虑使用像下面这样的 PhoneGap 插件,它使用本机代码来处理连接,但在 webviews 中启用(希望与标准兼容)WebSocket API

Android: https://github.com/anismiles/websocket-android-phonegap

安卓:https: //github.com/anismiles/websocket-android-phonegap

iPhone: https://github.com/remy/PhoneGap-Plugin-WebSocket

iPhone:https: //github.com/remy/PhoneGap-Plugin-WebSocket

Those plugins are just the first ones I found, not sure how much they are actively developed and stable

这些插件只是我发现的第一个插件,不确定它们的积极开发和稳定程度

回答by Shazron

So if the webpage opened with the file:// url protocol in PhoneGap was to send the header "Access-Control-Allow-Origin: *" -- theoretically it should all work with socket.io?

因此,如果在 PhoneGap 中使用 file:// url 协议打开的网页要发送标头“Access-Control-Allow-Origin: *”——理论上它应该都适用于 socket.io?

(it's possible to do so through NSURLProtocol, but I didn't want to go down this rabbit hole without knowing the fix)

(可以通过 NSURLProtocol 这样做,但我不想在不知道修复程序的情况下进入这个兔子洞)