1

我设置了一个节点服务器,它通过 socket.io 向客户端发送数据并在 phonegap 项目中使用它,

当我在浏览器和移动浏览器中测试它时它工作正常,但是当我构建我在模拟器中运行的 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 电话间隙

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

1 回答 1

1

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

var socket = io.connect('http://127.0.0.1:54321');
于 2013-09-24T18:20:42.810 回答