为什么我的node.js / socket.io应用程序无法在iOS6上运行? [英] Why does my node.js / socket.io app not work on iOS6?

查看:121
本文介绍了为什么我的node.js / socket.io应用程序无法在iOS6上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为socket.io的重点是不必担心现代浏览器? lol

I thought the whole point of socket.io was to not have to worry about modern browsers? lol

无论如何,我是套接字编程的新手。我有一个小应用程序,只是模仿鼠标移动。

Anyway, I am new to socket programming. I have a small app that simply mimics mouse movements.

您打开几个浏览器,当您移动鼠标时,您的操作将记录在其他浏览器中。它移动一个小方块。有点酷。但是,当我在iPad(iOS6)上打开它时什么都没有!套接字没有连接。我甚至在 connect 事件中放了一条警告信息,没有任何内容。

You open several browsers and when you move the mouse, your actions are recorded in the other browsers. It moves a small square. Kinda cool. However, when I open it on my iPad (iOS6) nothing! Sockets isn't connecting. I even put an alert message in the connect event and nothing.

在IE,FF和Chrome中工作就好了在我的笔记本上。唯一的区别是我的开发机器使用 localhost 而iPad使用我的机器的IP。但是,当我在笔记本电脑上连接到我的本地IP时,它仍然有效。只是不在Safari / iPad中。

Works in IE, FF and Chrome just fine on my laptop. The only difference is that my dev machine uses localhost while the iPad uses my machine's IP. However, when I connect to my local IP on my laptop, it still works. Just not in Safari/iPad.

这是我的服务器。

    var app = require('http').createServer(handler),
        io = require('socket.io').listen(app),
        fs = require('fs');


    app.listen(80);

    function handler(req, res) {
        var file = __dirname + '/public/index.html';
        fs.readFile(file, 
            function(err, data) {
                if(err) {
                    res.writeHead(500);
                    return res.end('Error loading index.html');
                }

                res.writeHead(200);
                res.end(data);
            }
        );
    }


    var rooms = ['abc', 'test1'];

    var sockets = [];
    io.sockets.on('connection', function(socket) {
        sockets.push(socket);

        socket.on('m', function(data) {
            socket.broadcast.to(socket.room).emit('relay', {msg: 'MouseX: ' + data.x + ' MouseY: ' + data.y, x: data.x, y: data.y});
        });

        socket.on('join', function(room) {
            socket.join(room);
            socket.emit('updateStatus', {msg: 'Joined room ' + room});
            console.log('Joined room ' + room);
        });

    });

这是我的客户:

<!doctype html>
<html>
    <head>
        <style>
            body {
                padding: 40px;
            }
            #cursor {
                background:white;
                border:1px solid black;
                color: white;
                display: block;
                height:24px;
                padding:6px;
                position:absolute;
                width:24px;
                z-index:20;
            }
        </style>
    </head>
    <body>

        <input id='msg' type='text' size='100' /><br />
        <input id='box' type='text' size='100' />
        <div id='cursor'></div>

        <script src='/socket.io/lib/socket.io.js'></script>
        <script>
            var socket = io.connect('http://localhost');
            var b = document.getElementById('box');
            var m = document.getElementById('msg');
            var c = document.getElementById('cursor');

            // join custom room
            socket.on('connect', function() {
                socket.emit('join', 'abc');
            });

            // update status messages from server
            socket.on('updateStatus', function(data) {
                m.setAttribute('value', data.msg);
            });

            socket.on('relay', function(data) {
                b.setAttribute('value', data.msg);
                c.style.left = parseInt(data.x) + 'px';
                c.style.top = parseInt(data.y) + 'px';
            });

            document.onmousemove = function(event) {
                event = event || window.event;
                socket.emit('m', {x: event.clientX, y: event.clientY});
            }


        </script>

    </body>
</html>


推荐答案

Localhost是本机的本地。你应该使用IP地址或域名:

Localhost is local to the machine. You're IP should use a ip address or domain name:

类似于:
io.connect('192.168.1.110');
或io.connect('test.myapp.com');

something like: io.connect('192.168.1.110'); or io.connect('test.myapp.com');

这篇关于为什么我的node.js / socket.io应用程序无法在iOS6上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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