流星:识别客户端并将数据发送到此特定客户端 - 在其他客户端上创建块元素

meteor: identify client and send data to this specific client - creating block-element on other clients

我的 meteor 应用程序必须由同一用户在多台 PC 上使用,但我必须防止同时使用该应用程序。问题是,用户会频繁更换工作场所,因此每次更换 PC 时都登录和注销并不是一个好主意。我想到了使用临时注销/阻止屏幕:

例子:有五台电脑。用户已在所有五台 PC 上登录该应用程序。现在他想在 PC 1 上工作:在所有其他四台 PC 上,应用程序将通过显示透明覆盖暂时被阻止。如果他要使用 PC 2,他可以通过键入一个小 PIN 来移除覆盖层(这比完全登录更快),然后他可以在这台 PC 上工作。

PC 1 将同时被阻止。

叠加层可能如下所示:

<div id="overlay" class="{{tempLogout}}">
    <input type="text" id="pin">
</div>

#overlay {
    display: none;
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.8);
}

#overlay.logout {
    display: block !important;
}

在我的助手中我会这样做:

Template.anything.helper({
    tempLogout: function() {
        return (Session.get('currentClient') == clientID) ? '' : 'logout';
    }
});

现在,如果 Session 变量为 false

,将显示叠加层

我的问题是如何让不同的客户端知道用户当前正在使用哪个客户端。并且此信息必须是反应性的。

所以在我的概念中,我会为每台 PC 创建一个随机 ID(也许已经存在任何选项来识别每个客户端??)并将此 ID 保存到 body-element 的数据属性中:

$('body').attr('data-id', Random.id());

通过输入正确的 PIN,此 ID 将保存到用户集合中:

var clientID = $('body').attr('data-id');
Session.set('currentClient', clientID);
Users.update({ _id: Meteor.id() }, { $set: { 'currentClient': clientID } });

通过将 Session 变量设置为新的客户端 ID,所有其他客户端都将被阻止,因为 Session 变量是被动的。在这里我不知道我必须使用哪个存储:Session 还是 Collection。

也许有人有更聪明的想法来完成这项工作。特别是我认为将 clientID 存储在数据属性中有点……hacky? 以及如何将客户端 ID 放入帮助程序 - 因为必须检查 currentClient 是否相同,然后检查客户端 ID...

我认为你的概念应该可行。 您应该使用 Meteor.user() 这样您就不必订阅任何内容(在您的示例中,您使用的是通用集合用户)。

您应该使用全局 clientID 并避免将其插入 html。 类似于:

  //on client
    clientID = Random.id(); //in Meteor a variable without var is global


Template.anything.helper({
    tempLogout: function() {
        return Meteor.user().profile.clientID == clientID ? '' : 'logout';
    }
});

当插入正确的引脚时:

   Meteor.users.update({_id: Meteor.userId()}, {$set: {'profile.clientID':clientID}});

如果您要使用的模板不止一个,则必须使用 Session 或全局模板助手 enable/disable。

我认为如果不使用 Meteor.logoutOtherClients() (http://docs.meteor.com/#/full/meteor_logoutotherclients) 这将非常困难。如果您使用诸如 oauth 之类的登录机制,则登录部分将是一个简单的按钮单击以返回到应用程序,这实际上比 pin 简单得多。