Html 如何在nodejs中通过TCP/IP获取传感器数据?
我有一个带有socket.io的nodejs应用程序。要测试这一点,请将下面的列表另存为app.js。安装node,然后npm安装socket.io,最后在命令提示符下运行:node app.js 此代码将数据发送到index.html文件。运行app.js后,在浏览器中打开此文件Html 如何在nodejs中通过TCP/IP获取传感器数据?,html,node.js,tcp,socket.io,sensors,Html,Node.js,Tcp,Socket.io,Sensors,我有一个带有socket.io的nodejs应用程序。要测试这一点,请将下面的列表另存为app.js。安装node,然后npm安装socket.io,最后在命令提示符下运行:node app.js 此代码将数据发送到index.html文件。运行app.js后,在浏览器中打开此文件 var socket=io.connect('//localhost:3000'); socket.on('welcome',函数(数据){ $('#messages').html(data.message); e
var socket=io.connect('//localhost:3000');
socket.on('welcome',函数(数据){
$('#messages').html(data.message);
emit('i am client',{data:'foo!'});
});
socket.on('time',函数(数据){
控制台日志(数据);
$('#messages').html(data.time);
});
on('error',function(){console.error(arguments)});
on('message',function(){console.log(arguments)});
现在发送的数据是当前时间,index.html工作正常,每五秒钟更新一次时间
我想修改代码,以便它通过TCP读取我的传感器数据。我的传感器通过数据采集系统连接,并通过IP:172.16.103.32端口7700中继传感器数据。(这是通过局域网进行的,因此您将无法访问该网络。)
如何在nodejs中实现这一点?
这是一个可行的选择吗?如果是这样的话,关于如何使用它有什么建议吗?我有一个不错的技巧,现在正在使用中,我请读者对此发表评论
参考资料:
那么,你的意思是说你想在node.js中接收传感器发送的数据,并向客户端发送数据?没错,也许还可以将数据存储在数据库中。我认为,如果你的传感器能够以某种方式将数据直接发送到运行node.js服务器的IP/端口,你的问题就可以得到解决。因此,您可以从“请求事件”获取发送到客户端的数据您好,我可以知道如何处理TCP通信中突然发生的网络错误或以太网断电事件吗?当出现网络错误时,错误事件侦听器上的回调没有被正确调用,如何处理这种情况,请帮助我-这是我在项目中遇到的问题之一,据我所知,NodeJS只能“捕获”报告给它的错误,因此,网络故障是一种与以太网断电不同的处理方式。您可以就此添加一个新问题。在这种情况下,我通常会在服务器崩溃时重新启动服务器。Node.js会在连接重置或类似的情况下通知错误,或者在服务器或客户端发送FIN数据包时终止连接-我所做的是在特定的时间间隔内ping并确保一切都在线(目前的解决方案)如果出现任何异常,我将重新连接/通知管理员
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);
<!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>
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);