【问题标题】:How to Listen data changes at NodeJs websocket如何在 NodeJs websocket 监听数据变化
【发布时间】:2017-02-23 10:21:17
【问题描述】:

在我的模块中,有一个 ClientServer1Server2

客户端向 Server1 请求一个 websocket Server2 向 Server1 传递消息

当 Server1 的消息发生变化时,我不知道如何在 Server1 上监听 请给我一些方法。

var temp ;
var aSocket ;
app.post( '/synReciver', function( req, res ) {
     console.log( 'I'm Server2, and I get Message from Server2' ) ;
     temp = req.headers ;
     res.end();
});

io.sockets.on('connection', function (socketIO) {
     socketIO.on('fromWebClient', function (webClientData) {
         aSocket[socketIO.id] = socketIO;
     });

     var interval= setInterval(function() {
       if ( temp[0] != null ) {
          aSocket[socketIO.id].emit('A message from server2 ' +temp[0] ) ;
       }
     },1000);

     socketIO.on('disconnect', function () {
         console.log('DISCONNECTED FROM CLIENT');
     });
 });

我希望套接字在连接时不要发出,而是等待临时数组改变

【问题讨论】:

  • Server2 会调用 /synReciver 来改变临时数组

标签: node.js websocket


【解决方案1】:

成功了

 var interval= setInterval(function() {
   aSocket[socketIO.id] = socketIO;

   if ( typeof(temp) != 'undefined' && temp != null ) {

      aSocket[socketIO.id].emit('pushToWebClient', temp);
      temp = {} ;
   } else {
         console.log('temp are null');
   }

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 2010-11-26
    • 2015-08-26
    • 1970-01-01
    • 2022-08-19
    • 2021-09-21
    相关资源
    最近更新 更多