socket.io连接到节点服务器的phonegap应用程序适用于浏览器但不适用于应用程序

时间:2013-09-23 15:16:41

标签: ios node.js cordova socket.io

我设置了一个节点服务器,它通过socket.io将数据发送到客户端并在手机缓冲区中使用它,

当我在浏览器中测试它时,它工作正常移动浏览器,但是当我构建一个我在模拟器中运行的ios应用程序时,它不起作用,

节点服务器代码的一部分:

define(['node-static','http'],function (Static,Http) {
return FileServer = function (server) {

    var httpFileServer = new Static.Server('./phonegap/www');

    Http.createServer(function (request, response) {
        request.addListener('end', function () {
            httpFileServer.serve(request, response);
        }).resume();
    }).listen(server.fileServerHTTPport,"0.0.0.0");

    return {
        // exampleVariable : function() {return exampleVariable},
        // exampleFunction : exampleFunction
    }
} });

index.html phonegap

... <script src="js/socket.io.js"></script>
<script src="js/jquery-2.0.3.min.js"></script>
<script>
var socket = io.connect('http://'+document.location.hostname+':54321');
   socket.on('display', function (data) {
      console.log(data.color);
      $("h1").text( data.color.h );
});
</script></head>
<body><h1> hello world</h1> ...

1 个答案:

答案 0 :(得分:1)

我想通了,我必须使用设备默认的localhost ip, 例如在ios:127.0.0.1

var socket = io.connect('http://127.0.0.1:54321');