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

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

问题描述

我正在尝试使用这个简单的教程:

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

我的 node.js 工作正常,我可以毫无问题地模拟到 iOS,但 socket.io 不工作,这是我的 javascript(与上面的教程相同):

<块引用>

app.initialize();document.addEventListener('deviceready', function() {控制台日志(套接字);socket.on('connect', function() {socket.on('文本',函数(文本){警报(文本);});});});

还有一件事,我怎样才能让这个 console.log 进行调试?

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

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

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

var server = require('http').createServer();var io = require('socket.io')(server);io.sockets.on('connection', function (socket) {console.log('socket 连接');socket.on('断开', 函数 () {console.log('socket 断开连接');});socket.emit('text', '哇.这样的事件.非常实时.');});服务器.听(3000);

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

解决方案

我做到了,这个教程很好,但并不完全正确.

您必须先将套接字连接到您的服务器(我使用的是本地主机和端口 3000,但如果您使用的是外部服务器,我认为您只需输入 ip 和端口):

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

之后,你调用socket.io",这是我的完整代码:

document.addEventListener('deviceready', function() {var socket = io.connect('http://localhost:3000');socket.on('connect', function() {socket.on('文本',函数(文本){警报(文本);});});});

I'm trying to use this simple tutorial:

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

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);
        });
    });
});

and one more thing how can i get this console.log to debug?

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>

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

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

解决方案

I made it, this tutorial is very good but it's not totally right.

You have to connect the socket to your server first (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');

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);
            });
        });
    });

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

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