为什么我的node.js/socket.io应用程序无法在iOS6上运行?

cbm*_*eks 10 mobile-safari node.js socket.io ios6

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

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

您打开了几个浏览器,当您移动鼠标时,您的操作将记录在其他浏览器中.它移动一个小方块.有点酷.但是,当我在iPad(iOS6)上打开它时什么都没有!套接字没有连接.我甚至在connect活动中发出警报信息,什么都没有.

在我的笔记本电脑上使用IE,FF和Chrome就好了.唯一的区别是我的开发机器localhost在iPad使用机器的IP时使用.但是,当我在笔记本电脑上连接到我的本地IP时,它仍然有效.只是不在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);
        });

    });
Run Code Online (Sandbox Code Playgroud)

这是我的客户:

<!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>
Run Code Online (Sandbox Code Playgroud)

小智 17

Localhost是机器的本地.您的IP应该使用IP地址或域名:

类似的东西:io.connect('192.168.1.110'); 或者io.connect('test.myapp.com');