我正在使用meteor ddp(分布式数据协议)到我的应用程序。服务器代码是使用java使用meteor和Android客户端编写的。 服务器代码
if (Meteor.isServer) {
Users = new Mongo.Collection('testUsers');
Meteor.publish('methodToListen', function(){
return Users.find();
});
}
客户端代码(https://github.com/delight-im/Android-DDP)
public class MainActivity extends AppCompatActivity implements MeteorCallback {
private Meteor mMeteor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Meteor.setLoggingEnabled(true);
mMeteor = new Meteor(this, "ws://192.168.137.1:3000/websocket");
mMeteor.addCallback(this);
mMeteor.connect();
}
@Override
public void onConnect(boolean signedInAutomatically) {
String subscriptionId = mMeteor.subscribe("methodToListen");
}
@Override
public void onDisconnect() {
}
@Override
public void onException(Exception e) {
}
@Override
public void onDataAdded(String collectionName, String documentID, String newValuesJson) {
Log.d("DATACHANGED", "Add " + collectionName + ", " + documentID + ", " + newValuesJson);
}
@Override
public void onDataChanged(String collectionName, String documentID, String updatedValuesJson, String removedValuesJson) {
}
@Override
public void onDataRemoved(String collectionName, String documentID) {
}
}
所以现在连接到服务器的每个客户端都会看到实时数据库更新。是否可以仅为特定客户端发送更新?因此,例如当用户登录系统时,它会获得唯一ID,并且基于该ID可以发送更新的数据吗?
更新我可以获得有关已连接客户端的信息
Meteor.onConnection(function(connection) {
console.log(connection.id + ", " + connection.clientAddress);
});
所以例如ip地址可以是id