使用socket.io与cordova和IOS设备 [英] using socket.io with cordova and IOS device

查看:729
本文介绍了使用socket.io与cordova和IOS设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个简单的教程:

I'm trying to use this simple tutorial:

http://socket.io/socket-io-with-apache-cordova/

我的node.js工作正常,我仿真到iOS没有问题,但socket.io不工作,这里是我的javascript(与上面的教程相同的方式):

My node.js is working fine and i'm emulating to iOS without problem, but the socket.io isn't working, here is my javascript(the same way as the tutorial above):


app.initialize();

document.addEventListener('deviceready', function() {
    console.log(socket);
    socket.on('connect', function() {
        socket.on('text', function(text) {
            alert(text);
        });
    });
});


.log来调试?

这里是我获得socket.io的方式(与上面的教程相同):

here is how i'm getting socket.io(the same way as the tutorial above):

<script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>

这里是我的server.js(和上面的教程一样):

here is my server.js(the same way as the tutorial above):

var server  = require('http').createServer();
var io      = require('socket.io')(server);

io.sockets.on('connection', function (socket) {
    console.log('socket connected');

    socket.on('disconnect', function () {
        console.log('socket disconnected');
    });

    socket.emit('text', 'wow. such event. very real time.');
});

server.listen(3000);

我认为问题和教程没有告诉我如何连接我的cordova应用程序与端口3000

I think, that the problem and the tutorial didn't told is how i would connect my cordova app with port 3000

推荐答案

我做到了,这个教程是非常好的,但它不完全正确,到您的服务器,所以你必须连接到您的服务像这样(我使用localhost和端口3000,但如果你使用一些服务器外,我想你只需把ip和端口) :

I made it, this tutorial is very good but it's not totally right, because will have to connect your socket to your server first, so you've to connect it to your serve like this(i'm using localhost and port 3000, but if you're using some server outside, i think you've to just put the ip and port):

var socket = io.connect('http://localhost:3000');

,然后调用socket.io,这里是我的完整代码:

and after that, you call "socket.io", here is my complete code:

document.addEventListener('deviceready', function() {
        var socket = io.connect('http://localhost:3000');
        socket.on('connect', function() {
            socket.on('text', function(text) {
                alert(text);
            });
        });
    });

这篇关于使用socket.io与cordova和IOS设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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