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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (362)

我有一个带有Socket.IO的NodeJS应用程序。要测试这一点,请将下面的清单保存为app.js。安装节点,然后NPM安装Socket.IO,最后在命令提示符上运行:nodeapp.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工作正常,每5秒更新一次。

我想修改代码,以便它通过TCP读取我的传感器数据。我的传感器通过数据采集系统连接,并通过IP:172.16.103.32端口:7700中继传感器数据。(这是通过局域网,所以你将无法访问。)

如何在NodeJS中实现这一点?

SensorMonkey一个可行的替代方案?如果是的话,对于如何使用它有什么建议吗?

提问于
用户回答回答于

我现在有一个很不错的黑客,我请读者对此发表评论.。


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. IO网站-www.socket.io-它现在是个热门词。
  2. TCP套接字编程
  3. NodeJS“网”模块
  4. 最简单的Socket.IO示例。

扫码关注云+社区

领取腾讯云代金券