我正在尝试将移动设备与应用程序的桌面部分分开,并认为我尝试使用DDP.connect作为移动应用程序与桌面应用程序共享数据的一种方式。
我的第一个障碍是有关流星的内部收藏和出版物。
我应该如何验证用户身份?我知道我可以调用login方法来对用户进行身份验证,但这仍然不能为我提供我习惯使用Meteor.users的所有其他出色的响应功能。
这应该工作吗,如果是这样,模式是什么。
谢谢
这是与远程服务器完全集成的功能(代码刷新除外,它会忘记用户会话)
if (Meteor.isClient) {
Meteor.connection = DDP.connect('http://remote.site.com');
Accounts.connection = Meteor.connection;
Meteor.users = new Meteor.Collection('users');
SomeCollection = new Meteor.Collection('remote_collection');
Meteor.connection.subscribe('users');
Meteor.connection.subscribe('remote_collection');
// rest if the code just as always
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以直接使用登录(通过基于帐户,通过帐户的帐户等),而无需调用登录方法。只需添加accounts-ui并包含{{>loginButtons}}它就可以了
我有一个类似的问题。我想在同一个后端使用两个不同的前端(尽管两者都用于桌面),因此它们可以使用相同的数据库,出版物和方法。在浏览了Meteor的源代码(1.1.0.3版)之后,我设法做到了如下。
1)启动后端服务器项目。
$ meteor --port 3100
Run Code Online (Sandbox Code Playgroud)
2)在前端项目中,在中放置以下内容server/server.config.js。
var backendUrl = process.env.BACKEND_URL;
if (backendUrl) {
__meteor_runtime_config__.BACKEND_URL = backendUrl;
__meteor_runtime_config__.ACCOUNTS_CONNECTION_URL = backendUrl;
console.log('config', __meteor_runtime_config__);
}
Run Code Online (Sandbox Code Playgroud)
3)在前端项目中,在中放置以下内容client/lib/client.connection.js。APS只是我的应用程序的名称空间。在使用订阅或方法之前,请确保已加载此lib文件(这就是它在文件夹中的原因)。
if (typeof APS == 'undefined') APS = {};
var backendUrl = __meteor_runtime_config__.BACKEND_URL;
if (backendUrl) {
APS.backendConnection = DDP.connect(backendUrl);
Meteor.connection = APS.backendConnection;
_.each(['subscribe', 'methods', 'call', 'apply', 'status', 'reconnect', 'disconnect'], function(name) {
Meteor[name] = _.bind(Meteor.connection[name], Meteor.connection);
});
console.log('connected to backend', APS.backendConnection);
}
Run Code Online (Sandbox Code Playgroud)
4)使用指向您的后端服务器的环境变量启动前端BACKEND_URL服务器。
$ BACKEND_URL=http://192.168.33.10:3100 meteor
Run Code Online (Sandbox Code Playgroud)
就这样。刷新客户端工作正常。而且我们不必摆弄Accounts.*。
更新:刚刚发现我的解决方案有问题。调用服务器方法时,this.userId始终为null。这是因为Meteor.connection和Accounts.connection是两个单独的连接,尽管相同BACKEND_URL。身份验证后,用户ID仅与后者相关联。固定client.connection.js如下。
if (typeof APS == 'undefined') APS = {};
var backendUrl = __meteor_runtime_config__.BACKEND_URL;
if (backendUrl) {
APS.originalConnection = Meteor.connection;
// Accounts is already connected to our BACKEND_URL
APS.backendConnection = Accounts.connection;
// Reusing same (authenticated) connection for method calls and subscriptions
Meteor.connection = APS.backendConnection;
_.each(['subscribe', 'methods', 'call', 'apply', 'status', 'reconnect', 'disconnect'], function(name) {
Meteor[name] = _.bind(Meteor.connection[name], Meteor.connection);
});
console.log('Connected to backend', APS.backendConnection);
}
Run Code Online (Sandbox Code Playgroud)