我正在使用Firebase java admin skd替换服务器端的Websocket机制。这是我的代码:
public class HelloWorld {
public static void main (String[] args) throws IOException {
FileInputStream serviceAccount =
new FileInputStream("/Users/d/firebase-adminsdk.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://alien-program-209719.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("/Call/1");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
System.out.println("this is the result" + dataSnapshot.getValue());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
}
我的问题是,我如何将代码作为服务器运行。因此,这里的想法是我想利用方法“ onChildAdded”来侦听将添加到我的数据库路径/ Call / 1中的任何元素。 那么我该如何做到这一点。谢谢。