如何在Meteor中存储特定于客户端的数据服务器端?

如何在Meteor中存储特定于客户端的数据服务器端?

问题描述:

Express实现了一个服务器端会话对象,可以存储特定于客户端的数据。你如何在Meteor中做同等的工作?

Express implements a server side session object that lets you store data specific to a client. How would you do the equivalent in Meteor?

建议使用一个集合。如果集合中的对象的id是在连接对象上公开服务器端和客户端的session_id,则这将起作用。

strack recommended using a collection. This would work if the ids of objects in the collection were session_ids that were exposed both server and client side on the connection objects.

客户端和服务器似乎通过客户端上的LivedataConnection共享session_id:

It appears the client and server share a session_id via the LivedataConnection on the client:

if (typeof (msg.session) === "string") {
  var reconnected = (self.last_session_id === msg.session);
  self.last_session_id = msg.session;
}

和服务器上的LivedataSession对象:

and the LivedataSession object on the server:

self.id = Meteor.uuid();

但是Meteor API不公开这些对象。访问会话信息的正确方法是什么?

But the Meteor API doesn't expose these objects. What is the correct way of accessing the session information?

如果客户端的Session对象与可访问的客户端唯一的服务器端Session对象同步,那将非常方便来自Meteor#publish和Meteor#methods。

It would be really convenient if a client's Session object synced with a server side Session object unique to the client that is accessible from Meteor#publish and Meteor#methods.

用户会话智能包我为Meteor写的就是为此而设计的。它提供了Meteor Session API的所有方法( setDefault 除外),还有一些额外的方法。它是被动的,所有的变化都是持久的。最重要的是,它可以在客户端和服务器上使用额外的 userId 参数。

The user-session smart package I wrote for Meteor is designed exactly for this. It provides all the methods of the Meteor Session API (with the exception of setDefault), and some additional ones as well. It's reactive, and all the changes are persistent. Best of all, it's available both on the client, and on the server with an additional userId argument.