10

is there a way to simulate lag with Meteor? Perhaps something that would delay all calls by say, 300ms?

4

4 回答 4

8

你可以这样做publish

Meteor._sleepForMs(5000); // sleeps for 5 seconds
于 2015-07-07T21:03:39.480 回答
5

我想我参加聚会有点晚了,但这里有一个更好的解决方案:

这个问题基本上有两个部分。一是如何延迟 Meteor WebSocket ( SockJS ) 的写入,一是如何延迟 HTTP 流量 ( connect )。您需要将以下两个片段添加到您的服务器端代码中,以延迟从 Meteor 服务器发送的所有流量。

网络套接字

困难的部分是覆盖 WebSocketwrite以延迟它setTimeout

(function () {
  // Set the delay you want
  var timeout = 3000
  // stream_server abstracts sockJS the DDP-Server talks to it. 
  var streamServer = Meteor.server.stream_server
  // The connect event listener
  var standardConnect = streamServer.server._events.connection

  // Overwrite the default event-handler
  streamServer.server._events.connection = function (socket) {
    // Overwrite the writes to the socket
    var write = socket.write
    socket.write = function () {
      var self = this
      var args = arguments
      // Add a delay
      setTimeout(function () {
        // Call the normal write methods with the arguments passed to this call
        write.apply(self, args)
      }, timeout)
    }
    // Call the normal handler after overwritting the socket.write function
    standardConnect.apply(this, arguments)
  }
})()

HTTP

使用connect它非常简单:

// Add a simple connect handler, wich calls the next handler after a delay
WebApp.rawConnectHandlers.use(function (req, res, next) {
  return setTimeout(next, timeout)
})
于 2015-09-08T18:58:14.250 回答
1

不确定所有调用,但您可以使用 Futures 在服务器上添加延迟,这样您就可以看到延迟补偿的实际效果。

例如,在流星方法中,您可以

Meteor.methods({
  post: function(post) {
    post.title = post.title + (this.isSimulation ? '(client)' : '(server)');

    // wait for 5 seconds
    if (! this.isSimulation) {
      var Future = Npm.require('fibers/future');
      var future = new Future();
      Meteor.setTimeout(function() {
        future.ret();
      }, 5 * 1000); // 5 seconds
      future.wait();
    }
    var postId = Posts.insert(post);
    return postId;
  }
});

这将显示插入的帖子末尾附加了 (client),然后 5 秒后将从服务器获取更新,帖子的标题将以 (server) 结尾

于 2013-04-30T14:39:41.597 回答
0

如果您想模拟订阅中的延迟,您可以执行以下操作:

Meteor.publish('collection', function(params) {
  Meteor._sleepForMs(2000); // Sleep for 2 seconds    
  return CollectionX.find(params.query,params.projection);
});
于 2015-07-07T21:07:59.417 回答