如何使用javafx的firebase

Mar*_*tin 6 javafx firebase

我正在构建一个桌面应用程序,我想使用直观的firebase API进行实时数据同步.我一直在网上搜索,但没有人指出要使用的Jars以及如何使用firebase应用程序进行配置.如果你能协助请给我一些步骤.我擅长以下步骤.我想为所有firebase操作创建一个帮助器类.

Jak*_*man 7

在客户端计算机上使用 admin SDK 是有风险的,因为它对您的数据库具有管理员权限。我通过创建一个 WebEngine 开发了一个解决方法,该 WebEngine 将读取一个没有正文的 html 文件,只有 firebase web 脚本。

<!DOCTYPE html>
<html>`
<head>
    <script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>

    <title></title>
</head>
<body>
</body>
<script>
    try {
        var config = {
          //firebase config here
        };

        firebase.initializeApp(config);
        var data = firebase.database();
        var ref = data.ref("/ref");
        ref.on("value", function (snapshot) {
            //pass snapshot to java
            alert(JSON.stringify(snapshot.val()));
        });
    } catch (error) {
        alert("Error - jse:" + error.message);
    }
</script>
</html>
Run Code Online (Sandbox Code Playgroud)

并在java中检索它

    //Start new webengine to run javascript.
    final WebEngine engine = new WebEngine();
    //get my html file.
    final URL url = NoficationTest.class.getResource("/javafx.html");
    //set to catch alerts from the javascript.
    engine.setOnAlert(event -> {
        try {

            final String data = event.getData();
            if (data != null) {
                if (data.contains("jse")) {
                      //handle if error in javascript.
                } else {
                      //handle if successfull
                }
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    });
    //Load html into webengine.
    engine.load(url.toString());
Run Code Online (Sandbox Code Playgroud)

javascript 有一种更好的方法来调用 java 方面的东西,我只是这样做是为了快速回答。我已经测试过这个并且它有效。