在浏览器页面刷新之前,流星流无法工作

Meteor stream not working until browser page refreshed

本文关键字:流星 工作 浏览器 刷新      更新时间:2023-09-26

我正在使用流在两个应用程序之间发送通知。应用程序通过redis通过集群进行通信。每当我把一些通知从一个应用程序推送到另一个应用程序时,它都不起作用。但是当我刷新浏览器并尝试时,它可以工作。

你能告诉我是什么问题吗?我的代码如下:

App 1代码

function sendMessage(message,date,toUserId,fromUserId,jobId){
  ChatStream.emit(toUserId,message,date,toUserId,fromUserId,jobId);
}
ChatStream.on(Meteor.userId(),function(message,date,toUserId,fromUserId,jobId){
  var formatDate = moment(date).fromNow();
  var index = ClientChat.find().count();
  var chatObj = {
    from:fromUserId,
    message:message,
    date:formatDate,
    jobId:jobId,
    index:index
  }
  //ClientChat.insert({from:fromUserId,message:message,date:formatDate,jobId:jobId,index:index})
  ClientChat.insert(chatObj);
  Session.set('receivedPing',chatObj);
})

App2

function sendMessage(message,date,toUserId,fromUserId,jobId){
  ChatStream.emit(toUserId,message,date,toUserId,fromUserId,jobId);
}
ChatStream.on(Meteor.userId(),function(message,date,toUserId,fromUserId,jobId){
  ClientChat.insert({from:fromUserId,message:message,date:date,jobId:jobId})
})

Cluster.js在server文件夹

Meteor.startup(function(){
  Meteor.Cluster.init();
  Meteor.Cluster.sync(ChatStream,LocalNotificationStream);
})

lib文件夹
ChatStream = new Meteor.Stream('chatStream');

我使用的包是arunoda流http://arunoda.github.io/meteor-streams/

我找到了一个解决方案。这是因为,代码

ChatStream.on(Meteor.userId(),function(message,date,toUserId,fromUserId,jobId){
  ClientChat.insert({from:fromUserId,message:message,date:date,jobId:jobId})
})

在应用程序刚刚启动且用户尚未登录时正在运行。因此,我确保只在用户登录时运行这段代码,执行以下命令

Deps.autorun(function(){
  if(Meteor.userId()){
         ChatStream.on(Meteor.userId(),function(message,date,toUserId,fromUserId,jobId){
      ClientChat.insert({from:fromUserId,message:message,date:date,jobId:jobId})
    })   
      }
    })
  }
});