【问题标题】:Refreshing web page in real time in sails.js在sails.js中实时刷新网页
【发布时间】:2015-10-25 14:26:48
【问题描述】:

在我的项目中,我使用的是sails.js。从 test1.ejs 我正在调用一个 Web 服务,该服务又使用 res.view() 调用另一个 ejs(test2.ejs)。 现在android用户正在输入一些影响数据库的值,需要实时反映在网页上。我无法弄清楚如何使用sails.js 来实现这一点。 此外,我什至需要显示 android 用户响应并同时刷新网页。简而言之,我想要一个动态的用户界面,比如股票市场,服务器上的任何变化都会反映在前端。 我还需要使用 angularjs 之类的东西吗?

【问题讨论】:

    标签: angularjs node.js sails.js


    【解决方案1】:

    如果我理解您的问题,您可以使用 JavaScript 接口。

    你应该这样创建类:

    public class WebAppInterface {
        Context mContext;
    
        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }
    
        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }
    

    在此之后,您应该像这样将此接口连接到您的 webview:

    webView.addJavascriptInterface(new WebAppInterface(this), "Android");
    

    现在你可以像这样从 JS 调用 Java 代码了:

    <script type="text/javascript">
        function showAndroidToast(toast) {
            Android.showToast(toast);
        }
    </script>
    

    或者像这样从 Java 调用 JS 代码:

    webview.loadUrl("javascript:window.showAndroidToast(\"Hello, World!\")");
    

    更多信息在这里:https://developer.android.com/guide/webapps/webview.html

    【讨论】:

    • 谢谢回复,但我面临的问题是如何自动刷新网页。移动应用也可以替换为其他网页。
    • 哦...对不起...我没有看标签并想到了Android :)
    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多