简体   繁体   中英

Get Response from Server Method when Client Loads page - Meteor

I have a 20 second timer that runs indefinitely on my Meteor server. When a user's client connects, is there a way for it to get the response from the Timer method on the server? Basically what I am trying to achieve is the ability for all users who connect to see the same timer running on their client.

So it short... I want a Client to get the output of a method that is continually running on the server and then output the response to the client. Is this possible?

Here is the timer code I am running on the server.

Meteor.methods({

  runTimer: function() {

    var running = false;
    var seconds = 20000; // (1 sec = 1000)
    var then; // Timer start time

    // ------------------------------------
    // Evaluate and route
    // ------------------------------------

    function router() {
      if (!running) {
        run();
      }
    };

    // ------------------------------------
    // Run the timer
    // ------------------------------------

    function run() {
      running = true;
      then = Date.now() + seconds;
      var interval = setInterval(function(){
        var time = parseTime(then-Date.now());
        if (time[0] > 0) {
          console.log(time[0] + '.' + time[1]);
        } else {
          console.log('0.00');
          running = false;
          clearInterval(interval);
          router();
        }
      }, 51);
    };

    // ------------------------------------
    // Parse time in MS for output
    // ------------------------------------

    function parseTime(elapsed) {
      // Array of time multiples [sec, decimal]
      var d = [1000,10];
      var time = [];
      var i = 0;
        while (i < d.length) {
          var t = Math.floor(elapsed/d[i]);
          // Remove parsed time for next iteration
          elapsed -= t*d[i];
          t = (i > 0 && t < 10) ? '0' + t : t;
          time.push(t);
          i++;
        } 
      return time;
    };

    router();

  }
});

Yes it's definitely possible, and the Meteor-way is to use the mongo/DDP/minimongo relationship to do it.

In otherwords, write the output to a collection, publish that to the clients, and Meteor will ensure that the latest output is always available to the clients.

So in your code, unless I'm mistaken I think you want to output this line to the clients?

console.log(time[0] + '.' + time[1]);

Create a collection:

Timestamp = new Mongo.Collection('timestamp')

Publish it (or use autopublish):

Meteor.publish("timestamp", function() {
  return Timestamp.find();
}

Insert/Update the document (you only need one) on the server:

Timestamp.upsert({_id: 1}, {$set: {timestamp: time[0] + '.' + time[1]}});

And on the client, read it.

Timestamp.findOne({_id: 1});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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