所以我目前有一个在我的 iPhone 上运行的 JavaScript 代码来提取它的加速计数据。我想做的是将此数据传递到我的 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>
最佳答案
在客户端你必须使用socket.emit('key', value);
在服务器端你必须监听一个键,例如在 nodeJS 中它是这样的
socket.on('key', function(value){});
关于javascript - 使用 Socket.io 传递 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448011/