使用Socket.io传递JavaScript变量

时间:2013-12-07 23:28:18

标签: javascript python ios socket.io

所以我目前有一个JavaScript代码,我在iPhone上运行以获取其Accelerometer数据。我想要做的是将此数据传递给我的Mac Book Pro并使用A python脚本中的变量。变量每秒都会改变多次,所以我在考虑使用Socket.io,有谁知道我会怎么做?我非常感谢能得到的任何帮助。谢谢。

<html>
<body>
<div id="content">
    <h1>Accelerometer JavaScript Test</h1>
<ul>
    <li>acceleration x: <span id="accelerationX"></span></li>
    <li>acceleration y: <span id="accelerationY"></span></li>
    <li>Motor Speed: <span id="speed"></span></li>
</ul>
</div>
<script type="text/javascript">
window.ondevicemotion = function(e){
        var x = e.accelerationIncludingGravity.x;
        var y = e.accelerationIncludingGravity.y;
        var newx = x * 100
        var newy = y * 100
        var finalx = Math.round(x);
        var finaly = Math.round(y);
        document.getElementById("accelerationX").innerHTML = finalx
        document.getElementById("accelerationY").innerHTML = finaly
        speed = finalx * 10
        document.getElementById("speed").innerHTML = speed
    }
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

在客户端,您必须使用socket.emit('key', value); 在服务器端,你必须监听一个密钥,例如在nodeJS中就像这样 socket.on('key', function(value){});

答案 1 :(得分:0)

哇,你在很多地方问过这个问题...... 我在这里回答:

Using Socket.io and iPhone Accelerometer