【问题标题】:How to emit the response streamed by a sensor using Socket.io, node.js如何使用 Socket.io、node.js 发出传感器流式传输的响应
【发布时间】:2019-11-08 02:38:27
【问题描述】:

我有一个 URL,它从传感器传输数据,例如温度和湿度。现在,如果我的 API 使用 socket.io 对特定路由进行调用,我需要获取数据流并将其显示在客户端仪表板的图表中。我对如何从传感器的 URL 获取响应以及如何在仪表板上显示为图表感到困惑。以下是我的示例代码,它以 json 对象的形式连续返回日期、小时、分钟和秒。我想将传感器数据的动态响应作为 json 对象在套接字发射函数中流式传输。

const express = require('express');
const app = express();
const port = 3000;
var url = require('url');
var fs = require('fs');
const server = require('http').createServer();
var link = "http:www.xample.com/usr/api/data/:deviceId";
server.listen(port,function(){
    console.log("Server Listening on port: "+port);
});

const io = require('socket.io')(server);
io.on('connection',function(socket){
setInterval(function(){
    socket.emit('date', 
    {
        'date': new Date().getDate(),
        'hour': new Date().getHours(),
        'minutes': new Date().getMinutes(),
        'seconds': new Date().getSeconds()
    });           
}, 1000);

socket.emit("welcome","Socket Connection is successful!");
});

const iot = require('socket.io-client');
let socket = iot.connect("http://localhost:3000");
socket.on("date",function(data){
    console.log(data);
});

socket.on("welcome",function(data){
    console.log("Connection Status: ", data);
});

【问题讨论】:

  • 对于您的服务器,您可以尝试socket.on('temperature', (data) => {socket.emit('client', data);}),对于客户端:socket.on('client', (data) => {console.log(data)});
  • 上面的代码(我的示例代码)是否正确?如果您不介意,请您在上面的代码中编辑并发布。请。我没有得到你的代码。 (数据)指的是什么?我怎样才能从我的链接中获得响应?
  • 我已经发布了一些示例代码,您可以使用它来开始。如果您有任何问题,请查看并告诉我
  • 嘿@radihuq,也请查看此链接。套接字工作正常,但有一个问题。所以,我又提出了一个问题。请看一看。 https://stackoverflow.com/questions/58767686/how-to-stop-the-duplication-of-data-on-client-side-of-socket-io-if-there-is-no-d

标签: node.js socket.io


【解决方案1】:

我强烈建议阅读 Socket.io Client API DocsServer API Docs

您可以开始使用的示例代码:

Client.js:

import React, {useState} from 'react';
import io from 'socket.io-client';

let socket;

const Client = () => {

    const [temperature, setTemperature] = useState(0);
    const [humidity, setHumidity] = useState(0);

    if (!socket) {
        socket = io('https://localhost:8080', {
            path: '/sensorInfo' 
        });

        socket.on('temperatureChange', (value) => {
            setTemperature(value);
        });

        socket.on('humidityChange', (value) => {
            setHumidity(value);
        })
    }

    return (
        <div>
            <p>The temperature logged is: {temperature}</p>
            <p>The humidity logged is: {humidity}</p>
        </div>
    );

}

export default Client;

Sensor.js:

const handleTemperatureChange = (v) => {
    let temperature = v;
    socket.emit('temperatureChange', {temperature});
}

const handleHumidityChange = (v) => {
    let humidity = v;
    socket.emit('humidityChange', {humidity});
}

这绝对不是一个完整的解决方案,因为它确实取决于您的系统设计,但希望它足以开始。

【讨论】:

  • 谢谢@radihuq。但实际上我需要有 sensor.js 文件代码,我必须使用上面示例代码中编写的 URL 来读取传感器数据。我不必在 html 页面中显示它们。
猜你喜欢
  • 1970-01-01
  • 2012-02-04
  • 2020-10-06
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
相关资源
最近更新 更多