简体   繁体   English

如何使 REST-API url 通过 Socket.IO 发出数据?

[英]How to make a REST-API url emit data via Socket.IO?

I am new to NodeJS/ExpressJS and Socket.io,my question may sound funny apologies for that.我是 NodeJS/ExpressJS 和 Socket.io 的新手,我的问题可能听起来很有趣。 As far as I have gone through the REST-API and Socket.io they have complete different applications.就我所经历的 REST-API 和 Socket.io 而言,它们有完整的不同应用程序。

But what if...但是如果……

I have a stream API that is streaming data from sensors, now I am saving the data in Database but apart from just saving the data in database only , if my API get a call on specific route then also stream that coming data to the client side also and save it to the database, when the client disconnect the connection then it simply saved to database and when client again makes a connection then the old unseen data is streamed and the stream continues to emit the data at client side.我有一个流 API,它从传感器流式传输数据,现在我将数据保存在数据库中,但除了仅将数据保存在数据库中之外,如果我的 API 在特定路由上获得调用,则还将即将到来的数据流式传输到客户端并将其保存到数据库中,当客户端断开连接时,它只是保存到数据库中,当客户端再次建立连接时,旧的看不见的数据被流式传输,流继续在客户端发出数据。

For eg : let say my rest-api have urls as -例如:假设我的rest-api的网址为-

  1. http://:/toWatch http://:/toWatch

  2. http://:/analyses http://:/分析

  3. http://:/showLiveSensersData http://:/showLiveSensersData

Now the first 2 urls have straight forward approach to send response.现在前 2 个 url 有直接的方法来发送响应。

But on the third url, I want to stream the data to client via socket.但是在第三个 url 上,我想通过套接字将数据流式传输到客户端。 Now my question is, if the approach is ok and if it can be done then how?现在我的问题是,如果该方法可以,并且可以完成,那么如何完成? Well what I saw was that the Socket.io instance can not be accessed in route file of expressjs.那么我看到的是在expressjs的路由文件中无法访问Socket.io实例。

Any suggestion and solution or help will be much appreciated.任何建议和解决方案或帮助将不胜感激。

You might be able to pass your instance of io to other your routes through the request object.您可以通过请求对象将 io 实例传递给其他路由。 Something like this:像这样的东西:

app.use(function(req,res,next){ req.io = io; next(); });

and then reference it in your routes using req.io.然后使用 req.io 在你的路由中引用它。 Not sure if there are problems with using it this way, but I have done it before and it has worked with express router.不确定以这种方式使用它是否有问题,但我之前已经这样做过并且它已经与 express router 一起使用了。

Even I am struggling with the same functionality as you had faced earlier. 甚至我都在为您以前遇到的相同功能而苦苦挣扎。 I was confused how to fetch the responses from the URL of my sensor and how to return them so that I could use that to display as charts on dashboard. 我很困惑如何从传感器的URL中获取响应以及如何返回响应,以便可以将其显示为仪表板上的图表。 Can you please help me with the sample code? 您能帮我提供示例代码吗? But before that, please do check my sample code here. 但在此之前,请在此处检查我的示例代码。 This is Server.js file. 这是Server.js文件。 I want to get the dynamic response of link-variable and send that to the emit event. 我想获得链接变量的动态响应,并将其发送到发出事件。

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM