簡體   English   中英

是否有任何類型的方法可以從 node.js 服務器中的 HTML 服務器的腳本中調用 function?

[英]Is there any type of way to call a function inside a script in a HTML server from with node.js?

提前抱歉我缺乏技術語言!

所以,我想做的是一個用於在 OBS(開放廣播軟件)中使用的 twitch 訂閱計數器,我制作了實際計算 node.js 中的訂閱數的部分(我實際上讓它們保存發生了多少訂閱的數量在文本文件中)。 我缺少的是將在 OBS 上顯示的部分,這是我使用 html 和 CSS 制作的實際圖形計數器。 I made so that when I run the node.js file it makes a local web server (localhost:etc) using the http.createServer().listen(port) function with the html in it. 問題是我需要在 HTML 文件中調用 java 腳本腳本中的 function ( setValue() ),這會改變計數器狀態嗎?

html 腳本代碼:

<script type="text/javascript">

    let reader = new FileReader();



    class ProgressBar {
        constructor(element, initialValue = 0) {
            this.valueElem = element.querySelector('.progress-bar-value');
            this.fillElem = element.querySelector('.progress-bar-fill');

            this.setValue(initialValue); 
        }

        setValue(newValue) {
            if(newValue < 0){
                newValue = 0;
            }

            if(newValue > 100){
                newValue = 100;
            }

            this.value = newValue
            this.update();
        }

        update() {
            const percentage = this.value + '%';

            this.fillElem.style.width = percentage;
            this.valueElem.textContent = percentage;
        }
    }

    var pb1 = new ProgressBar(document.querySelector('.progress-bar'),50);
</script>

你可以使用 WebSockets

WebSockets 是 Web 應用程序中 HTTP 通信的替代方案。

它們在客戶端和服務器之間提供了一個長壽命的雙向通信通道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM