Node.js 如何将套接字数据发送到节点js中的特定客户端(基于deviceId)?

Node.js 如何将套接字数据发送到节点js中的特定客户端(基于deviceId)?,node.js,express,mongoose,socket.io,Node.js,Express,Mongoose,Socket.io,我有一个有很多模板的系统。模板有多个关联的传感器。每个传感器将数据发送到服务器并存储在数据库中。每个传感器都有一个唯一的deviceId。数据存储在Mongo数据库中相应的模板集合中 例如,有两个设备(sbdth11-001、sbdth11-002)和“sbdth11-001”发送数据,而“sbdth11-002”保持空闲。在前端,当我看到“sbdth11-001”的网格时,我正在获取数据。但当我看到“sbdth11-002”的网格时,它得到的是“sbdth11-001”的数据。这意味着服务器套

我有一个有很多模板的系统。模板有多个关联的传感器。每个传感器将数据发送到服务器并存储在数据库中。每个传感器都有一个唯一的deviceId。数据存储在Mongo数据库中相应的模板集合中

例如,有两个设备(sbdth11-001、sbdth11-002)和“sbdth11-001”发送数据,而“sbdth11-002”保持空闲。在前端,当我看到“sbdth11-001”的网格时,我正在获取数据。但当我看到“sbdth11-002”的网格时,它得到的是“sbdth11-001”的数据。这意味着服务器套接字向连接到它的所有客户端发送数据。谁能帮忙解决这个问题。请看一下我的代码

服务器端 Server.JS

const server = http.createServer(app);
global.io = require('socket.io')(server);
io.on('connection', function(socket){
//console.log('a user connected');
});
router.post("/upload/device/data/:tempId", TemplateController.AddDeviceData);
AddDeviceData:async function(req, res){
    let err, deviceLog;

    [err, deviceLog]=await to(TemplateService.AddDeviceLogs(req.params.tempId, req.body));
    if(err) return res.serverError(err.message);
    if(deviceLog&&deviceLog!==false){
        return res.ok(deviceLog);
    }else{
        res.badRequest("Sorry cannot add Device log data");
    }
}
AddDeviceLogs:async function(templateId, payload){
    let err, deviceData;

    payload.template=templateId;
    payload.entryDayTime=new Date();
    const myCollection=templateId;
    [err, deviceData]=await to(mongoose.connection.db.collection(myCollection).insert(payload));
    if(err) TE(err.message, true);
    //Socket emit code begin
    if(deviceData){
        let err,singleDeviceData,allDevicesData;;

        [err,singleDeviceData]= await to(this.displayLogs(templateId,payload.deviceId));
        [err,allDevicesData] = await to(this.displayLogs(templateId));
        if(err) io.emit('socketError', err.message);
        io.emit('singleDeviceData',singleDeviceData); //Has to emit the data to 
                    //the device of template whose IDs matches 
                    //with the IDs in this.displayLogs(templateId,payload.deviceId) 
        io.emit('allDevicesData',allDevicesData);//Has to emit the data to 
                    //the template whose ID matches 
                    //with the ID in this.displayLogs(templateId)
    }
    //Socket emit code end
    return (deviceData)? deviceData.result:false;
}
io.emit(`singleDeviceData${templateId}${payload.deviceId}`,singleDeviceData); //Emitting the device data of a template

io.emit(`allDevicesData${templateId}`,allDevicesData); //Emitting the data of all devices(template)
Routes.JS

const server = http.createServer(app);
global.io = require('socket.io')(server);
io.on('connection', function(socket){
//console.log('a user connected');
});
router.post("/upload/device/data/:tempId", TemplateController.AddDeviceData);
AddDeviceData:async function(req, res){
    let err, deviceLog;

    [err, deviceLog]=await to(TemplateService.AddDeviceLogs(req.params.tempId, req.body));
    if(err) return res.serverError(err.message);
    if(deviceLog&&deviceLog!==false){
        return res.ok(deviceLog);
    }else{
        res.badRequest("Sorry cannot add Device log data");
    }
}
AddDeviceLogs:async function(templateId, payload){
    let err, deviceData;

    payload.template=templateId;
    payload.entryDayTime=new Date();
    const myCollection=templateId;
    [err, deviceData]=await to(mongoose.connection.db.collection(myCollection).insert(payload));
    if(err) TE(err.message, true);
    //Socket emit code begin
    if(deviceData){
        let err,singleDeviceData,allDevicesData;;

        [err,singleDeviceData]= await to(this.displayLogs(templateId,payload.deviceId));
        [err,allDevicesData] = await to(this.displayLogs(templateId));
        if(err) io.emit('socketError', err.message);
        io.emit('singleDeviceData',singleDeviceData); //Has to emit the data to 
                    //the device of template whose IDs matches 
                    //with the IDs in this.displayLogs(templateId,payload.deviceId) 
        io.emit('allDevicesData',allDevicesData);//Has to emit the data to 
                    //the template whose ID matches 
                    //with the ID in this.displayLogs(templateId)
    }
    //Socket emit code end
    return (deviceData)? deviceData.result:false;
}
io.emit(`singleDeviceData${templateId}${payload.deviceId}`,singleDeviceData); //Emitting the device data of a template

io.emit(`allDevicesData${templateId}`,allDevicesData); //Emitting the data of all devices(template)
TemplateController.JS

const server = http.createServer(app);
global.io = require('socket.io')(server);
io.on('connection', function(socket){
//console.log('a user connected');
});
router.post("/upload/device/data/:tempId", TemplateController.AddDeviceData);
AddDeviceData:async function(req, res){
    let err, deviceLog;

    [err, deviceLog]=await to(TemplateService.AddDeviceLogs(req.params.tempId, req.body));
    if(err) return res.serverError(err.message);
    if(deviceLog&&deviceLog!==false){
        return res.ok(deviceLog);
    }else{
        res.badRequest("Sorry cannot add Device log data");
    }
}
AddDeviceLogs:async function(templateId, payload){
    let err, deviceData;

    payload.template=templateId;
    payload.entryDayTime=new Date();
    const myCollection=templateId;
    [err, deviceData]=await to(mongoose.connection.db.collection(myCollection).insert(payload));
    if(err) TE(err.message, true);
    //Socket emit code begin
    if(deviceData){
        let err,singleDeviceData,allDevicesData;;

        [err,singleDeviceData]= await to(this.displayLogs(templateId,payload.deviceId));
        [err,allDevicesData] = await to(this.displayLogs(templateId));
        if(err) io.emit('socketError', err.message);
        io.emit('singleDeviceData',singleDeviceData); //Has to emit the data to 
                    //the device of template whose IDs matches 
                    //with the IDs in this.displayLogs(templateId,payload.deviceId) 
        io.emit('allDevicesData',allDevicesData);//Has to emit the data to 
                    //the template whose ID matches 
                    //with the ID in this.displayLogs(templateId)
    }
    //Socket emit code end
    return (deviceData)? deviceData.result:false;
}
io.emit(`singleDeviceData${templateId}${payload.deviceId}`,singleDeviceData); //Emitting the device data of a template

io.emit(`allDevicesData${templateId}`,allDevicesData); //Emitting the data of all devices(template)
TemplateService.JS

const server = http.createServer(app);
global.io = require('socket.io')(server);
io.on('connection', function(socket){
//console.log('a user connected');
});
router.post("/upload/device/data/:tempId", TemplateController.AddDeviceData);
AddDeviceData:async function(req, res){
    let err, deviceLog;

    [err, deviceLog]=await to(TemplateService.AddDeviceLogs(req.params.tempId, req.body));
    if(err) return res.serverError(err.message);
    if(deviceLog&&deviceLog!==false){
        return res.ok(deviceLog);
    }else{
        res.badRequest("Sorry cannot add Device log data");
    }
}
AddDeviceLogs:async function(templateId, payload){
    let err, deviceData;

    payload.template=templateId;
    payload.entryDayTime=new Date();
    const myCollection=templateId;
    [err, deviceData]=await to(mongoose.connection.db.collection(myCollection).insert(payload));
    if(err) TE(err.message, true);
    //Socket emit code begin
    if(deviceData){
        let err,singleDeviceData,allDevicesData;;

        [err,singleDeviceData]= await to(this.displayLogs(templateId,payload.deviceId));
        [err,allDevicesData] = await to(this.displayLogs(templateId));
        if(err) io.emit('socketError', err.message);
        io.emit('singleDeviceData',singleDeviceData); //Has to emit the data to 
                    //the device of template whose IDs matches 
                    //with the IDs in this.displayLogs(templateId,payload.deviceId) 
        io.emit('allDevicesData',allDevicesData);//Has to emit the data to 
                    //the template whose ID matches 
                    //with the ID in this.displayLogs(templateId)
    }
    //Socket emit code end
    return (deviceData)? deviceData.result:false;
}
io.emit(`singleDeviceData${templateId}${payload.deviceId}`,singleDeviceData); //Emitting the device data of a template

io.emit(`allDevicesData${templateId}`,allDevicesData); //Emitting the data of all devices(template)
设备sbdth11-001的网格 设备sbdth11-002的网格


两个网格获得相同的数据。如果设备未发送任何数据,则应在网格中不呈现任何内容。

控制台记录此
套接字
变量。它应该有一个ID。保存此ID

io.on('connection', function(socket){
//console.log('a user connected');
});
要仅向此客户端发送,请执行以下操作:

  io.to(`${socketId}`).emit('hey', 'I just met you'); 

控制台记录此
socket
变量。它应该有一个ID。保存此ID

io.on('connection', function(socket){
//console.log('a user connected');
});
要仅向此客户端发送,请执行以下操作:

  io.to(`${socketId}`).emit('hey', 'I just met you'); 

我有办法了。我们只需要将deviceId和socket emit事件一起发送到客户端套接字

在我的用例中,我在一个模板中有许多设备。模板ID是唯一的,模板中的设备ID应该是唯一的。但是一个deviceId可以在不同的模板中多次使用,记住deviceId在模板级别应该是唯一的

这是代码

服务器代码(Node.JS)

在前端,我们需要在将数据呈现到网格之前比较deviceId和templateId

我们发出这样的事件,
singleDeviceData566567586Device-1
。这里,
singleDeviceData
是事件名称,
566567586
是模板ID,
Device-1
是设备ID


因此,在客户端,在侦听
singleDeviceData
事件时,我们将
566567586
Device-1
与当前在浏览器上打开的网页的templateId和deviceId进行比较。如果两者相同,则会呈现数据,并且随着数据不断插入数据库,它会不断更新。否则,将显示此页面上应显示的内容,数据将不会更新

我找到了解决办法。我们只需要将deviceId和socket emit事件一起发送到客户端套接字

在我的用例中,我在一个模板中有许多设备。模板ID是唯一的,模板中的设备ID应该是唯一的。但是一个deviceId可以在不同的模板中多次使用,记住deviceId在模板级别应该是唯一的

这是代码

服务器代码(Node.JS)

在前端,我们需要在将数据呈现到网格之前比较deviceId和templateId

我们发出这样的事件,
singleDeviceData566567586Device-1
。这里,
singleDeviceData
是事件名称,
566567586
是模板ID,
Device-1
是设备ID


因此,在客户端,在侦听
singleDeviceData
事件时,我们将
566567586
Device-1
与当前在浏览器上打开的网页的templateId和deviceId进行比较。如果两者相同,则会呈现数据,并且随着数据不断插入数据库,它会不断更新。否则,将显示此页面上应显示的内容,数据将不会更新

谢谢你的帮助。但我现在有多台设备连续发送数据。如何将设备发送到自身的数据发送到前端进行渲染?如果您使用的是文件室,则
io.in('nameofromm').emit('singleDeviceData',singleDeviceData)或。。。您可以将另一个
添加到()
当用户连接时,您可以像下面这样为它设置一个房间
socket.join('NameOfRoom')然后。您可以使用此emit向该房间中的每个人发送,包括发送者
io.in('nameorfromm')。emit('singleDeviceData',singleDeviceData)谢谢你的帮助。但我现在有多台设备连续发送数据。如何将设备发送到自身的数据发送到前端进行渲染?如果您使用的是文件室,则
io.in('nameofromm').emit('singleDeviceData',singleDeviceData)或。。。您可以将另一个
添加到()
当用户连接时,您可以像下面这样为它设置一个房间
socket.join('NameOfRoom')然后。您可以使用此emit向该房间中的每个人发送,包括发送者
io.in('nameorfromm')。emit('singleDeviceData',singleDeviceData)