html - 如何在 nodejs 中通过 TCP/IP 获取传感器数据?

标签 html node.js tcp socket.io sensors

我有一个带有 socket.io 的 nodejs 应用程序。 要对此进行测试,请将以下 list 保存为 app.js。安装 node,然后 npm install socket.io 最后在命令提示符下运行:node app.js

var http = require('http'),
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/index.html');

// Send index.html to all requests
var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
});

// Socket.io server listens to our app
var io = require('socket.io').listen(app);

// Send current time to all connected clients
function sendTime() {
    io.sockets.emit('time', { time: new Date().toJSON() });
}

// Send current time every 10 secs
setInterval(sendTime, 5000);

// Emit welcome message on connection
io.sockets.on('connection', function(socket) {
    socket.emit('welcome', { message: 'Welcome!' });

    socket.on('i am client', console.log);
});

app.listen(3000);

此代码将数据发送到文件 index.html。 运行 app.js 后,在浏览器中打开此文件。

<!doctype html>
<html>
    <head>
        <script src='http://code.jquery.com/jquery-1.7.2.min.js'></script>
        <script src='http://localhost:3000/socket.io/socket.io.js'></script>
        <script>
            var socket = io.connect('//localhost:3000');

            socket.on('welcome', function(data) {
                $('#messages').html(data.message);

                socket.emit('i am client', {data: 'foo!'});
            });
            socket.on('time', function(data) {
                console.log(data);
                $('#messages').html(data.time);
            });
            socket.on('error', function() { console.error(arguments) });
            socket.on('message', function() { console.log(arguments) });
        </script>
    </head>
    <body>
        <p id='messages'></p>
    </body>
</html>

现在发送的数据是当前时间,index.html 工作正常,每五秒更新一次时间。

我想修改代码,让它通过 TCP 读取我的传感器数据。我的传感器通过数据采集系统连接,并通过 IP:172.16.103.32 端口:7700 中继传感器数据。 (这是通过 LAN 进行的,因此您无法访问。)

如何在 nodejs 中实现?

SensorMonkey一个可行的选择?如果是这样,关于如何使用它的任何指示?

最佳答案

我有一个不错的 hack,现在正在工作,我请求读者对此发表评论......


var net = require('net'),
http = require('http'),
port = 7700,                    // Datalogger port
host = '172.16.103.32',         // Datalogger IP address
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/index.html');

// Send index.html to all requests
var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
});

// Socket.io server listens to our app
var io = require('socket.io').listen(app);

// Emit welcome message on connection
io.sockets.on('connection', function(socket) {
    socket.emit('welcome', { message: 'Welcome!' });

    socket.on('i am client', console.log);
});

//Create a TCP socket to read data from datalogger
var socket = net.createConnection(port, host);

socket.on('error', function(error) {
  console.log("Error Connecting");
});

socket.on('connect', function(connect) {

  console.log('connection established');

  socket.setEncoding('ascii');

});

socket.on('data', function(data) {

  console.log('DATA ' + socket.remoteAddress + ': ' + data);
  io.sockets.emit('livedata', { livedata: data });        //This is where data is being sent to html file 

});

socket.on('end', function() {
  console.log('socket closing...');
});

app.listen(3000);

引用资料:

  1. Socket.io 网站 - www.socket.io - 现在是流行语。
  2. TCP Socket Programming
  3. Nodejs "net" module
  4. Simplest possible socket.io example.

关于html - 如何在 nodejs 中通过 TCP/IP 获取传感器数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22345290/

相关文章:

Node.js 将数组转换为对象数组

c++ - 寻找 C(++) 中的基本反向 Shell 示例

linux - 在 tcp "long"连接和 "short"连接之间选择内部服务

javascript - 在窗口加载时创建显示隐藏功能?

javascript - 如何使用单选按钮更改文本大小 - JQuery Mobile

javascript - 使用相同的 javascript 自动填充多个 div

javascript - 根据容器的位置在滚动上绘制 svg

javascript - 如何在 V8 中将对象传递给 JavaScript 回调

javascript - 使用增量参数循环 GET 请求

tcp - 应用层PDU的最大尺寸