【问题标题】:In node.js How to send app.post to client by custom event?在 node.js 如何通过自定义事件将 app.post 发送到客户端?
【发布时间】:2018-01-31 01:34:07
【问题描述】:

客户端通过按下按钮发送一个发布请求。 (服务器端:开始扫描发现蓝牙外设)

之后,我们想从服务器发送一个res.send("BLE_name"),该函数正在寻找蓝牙外围设备,但响应在客户端按下按钮后很长时间才到达。

假设:我想在我自己的自定义事件之后调用 app.post... 如何创建自定义事件来调用 app.post()

我们使用:

  • node.js
  • 贵族模块
  • 快递
// 向客户端发送数据 //蓝牙外设列表 // JSON 对象 app.post('/resScan', function(req, res){ res.send(JSON_Object); });

问候和非常感谢,

菲利普

【问题讨论】:

  • 您是否尝试在客户端触发POST 请求, 客户端按下按钮?
  • 我认为他正试图从服务器发送一个 POST(??)如果是这样的话,有些东西会很坏
  • 尝试 res.end("BLE_name") 而不是 res.send("BLE_name")。或 res.send("BLE_name").end()
  • @slesh 我不认为这与res.send() 函数有关,他要么默认尝试从服务器发送POST 请求,我不知道他想怎么做完成它,或者他想从客户端触发它。
  • @Jack Delson,“但响应在按下按钮后很长时间才到达”,这是因为 .send() 调用未完成响应过程。无论如何,他必须尝试 res.send("BLE_name").end()

标签: node.js bluetooth bluetooth-lowenergy


【解决方案1】:

和我的朋友我们找到了一个解决方案,但不确定是否是解决方案。

const formClientName = "form_03_btn";

var my_res_global    = null;

app.get('/', function( req, res )
{
  // We update the res value each time
  // we have a app.post method
  my_res_global = res;
  res.render( formClientName );
}); // app.get('/', function( req, res ))

app.post('/arduino_cmd', function( req, res )
{
  // We update the res value each time
  // we have a app.post method
  my_res_global = res;

  commandLine = req.body.content;
  sendCommandToArduino( commandLine );
  res.render( formClientName );
}); // app.post('/arduino_cmd', function( req, res ))

[...]

dialogCharacteristic[dialogCharacteristic.length-1].on('data', function(data, isNotification )
{
  [...]

  my_res_global.render( formClientName,  { displayArduino: "My test works fine !" } );

  [...]

});

效果很好!

现在如果我们这样写就会出错:

dialogCharacteristic[dialogCharacteristic.length-1].on('data', function(data, isNotification )
{
  [...]

  var myData = "My test works fine !";

  my_res_global.render( formClientName,  { displayArduino: myData } );

  [...]

});

给出错误: 错误:发送后无法设置标头。

我们正在寻找解决方案...

非常感谢大家的时间、问题和想法!

最好的问候,

菲利普。

【讨论】:

    【解决方案2】:

    不,我和他一起工作,事实上我们有一个问题:

    按钮正常工作并正确调用服务器。我们也正确收到了“req”,但我们调用了另一个函数(带有一个事件),它需要一个随机时间,然后这个函数完成,然后我们想为响应开具发票,如下所示:

    noble.on('discover', function(){
         //....many statements and others process here
         // we use req.body.content  here
    
         // and at the end
    
         app.post('/server.js', function(req, res){
                res.send('data-to-the-client');
         });
    });
    

    我们的问题更多:随着事件的发生?并告诉好吧,所有事件都已完成,现在为资源开具发票。

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多