javascript - Meteor框架免费服务器崩溃

标签 javascript meteor

嗨,我刚刚学习了 Meteor 并开发了一款多人游戏。出于测试目的,我部署到免费服务器,每天晚上我们和一些 friend 一起玩游戏,通常有 20 个在线人。然而,网站有时会崩溃,没有数据进出。它只是卡住了。我检查了客户端日志和服务器日志,但什么也没有。当我刷新页面时,它卡住在加载模板上。

我使用铁路由器作为加载屏幕:

Router.configure({
  layoutTemplate: 'main',
  notFoundTemplate: 'notFound',
  loadingTemplate: 'loading'
});

Router.route('/loading', {
  template : 'loading'
});

Router.route('/', {
  template : 'home',
  waitOn: function() {
    return [Meteor.subscribe('chat'),Meteor.subscribe('games'),Meteor.subscribe('users')];
  },  
  data : function(){
    return {
      chat : Chat.find({},{sort: {createdAt: 1}}),
      games : Games.find({},{sort: {createdAt: -1}}),
      users : Meteor.users.find({},{sort : {"status.online":-1}})
    }
  }
});

我不知道这个问题。有时在主页上卡住,有时在游戏页面上卡住。对于游戏页面,我正在简化我的代码以便您理解。

游戏中有两个关卡:白天和黑夜。它从游戏创建者的点击开始,早晨从倒计时开始。当倒计时结束时,夜晚就会开始相应的倒计时。并且它会继续循环直到游戏结束。早上结束时,游戏会进行一些计算,然后切换到晚上。夜晚也一样。

为了倒计时,我将 setTimeOut() 函数与 Fiber 结合使用。

Meteor.methods({
    startGame : function(id){
        //some other code here
        //....

        //set timestamp of the end date
        var currentDate = new Date();
        console.log("currentDate: "+currentDate);
        if(night){ var interval = durationNight;}
        else{ var interval = durationMorning;}
        console.log("interval: "+interval);
        var endDate = currentDate.setSeconds(currentDate.getSeconds() + interval);
        console.log("endDate: "+endDate);

        Games.update({"_id" : id}, {$set : {"endTime" : endDate}});


        endLevelInternal[id] = setTimeout(function () {
            // // completed
            Fiber(function() { 
                console.log(id +' countdown completed');

                endLevelFlag[id] = true;

                if(endLevelFlag[id]){
                    endLevelFlag[id] = false;
                    Meteor.call('endLevel', id, function(){
                        endLevelFlag[id] = true;
                    });
                }
            }).run();  

        }, interval*1000);
    },

    endLevel : function(){
        //some other code here
        //...

        //
        if(!finished){
            Meteor.call('startGame', id);   
        }
    }
});

Template.game.events({
    'click .startGame' : function(evt, template){
        var flag = true;
        if(flag){
            flag = false;
            Meteor.call('startGame', template.data._id, function(){
                flag = true;
            });
        }
    }
});

我不确定问题是由于服务器代码还是由于免费服务器(RAM 或 CPU)的限制。

最佳答案

我建议查看 Meteor 日志,或部署在其他已准备好生产的平台上。 一种选择是使用 Meteor Up 的 AWS 或 DigitalOcean。

关于javascript - Meteor框架免费服务器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270465/

相关文章:

javascript - 为 slider 文本调用 javascript 函数

onclick 的 Javascript 函数

javascript - Session 值应该在 Meteor 应用程序中的哪里启动?

javascript - 铁 :router - "Couldn' t find a template named 'loading' . ..”

meteor - 从 TypeScript 声明全局命名空间变量

javascript - Ember - 计算属性(这是一个数组)在将项目添加到数组时不获取更新值

javascript - 你可以在不使用 document.write() 的情况下销毁文档元素和事件监听器吗?

meteor - meteor 收集更新中的狩猎错误

javascript - Meteor/Node.js : Multiple http requests within a for loop, 在时间间隔内均匀分布?

javascript - 如何访问数组中的数组元素(JavaScript)?