简体   繁体   中英

Use HTML5 localstorage to exchange information

I have a program written in c++ that reads values from this board . Anyways that part is not important. What I have is data that is constantly changing and I will like to graph that data. I was hoping to use a web browser to display the data since there are so many open source graphs and charts out there written in JavaScript. So my problem is to send data to the browser from my c++ program

I already investigated and UDP is not available in browsers yet so I will have to use TCP. TCP websockets are not that fast and I was thinking about using html5 localstorage instead. By that I mean have my c++ program write to the database on localStorage then javascript will wait for the value of that variable to exist and invent some sort of protocol that will make that work. Local storage is really fast for example :

<script type="text/javascript">

    var counter = 0;

    window.onload = function () {

        function Test() {

            counter++;

            localStorage.p = counter + ""; // perform write 

            var read = localStorage.p; // perform read

            if (read == "5000")
                alert((new Date() - now)); // shows 45
            else
                Test(); // loop again
        }

        var now = new Date(); 
        Test();
    }

</script>

that script takes 54 milliseconds and it reads AND writes 5000 times! That means that instead of creating a plug-in for the browser next time I will just implement some sort of protocol that will enable me to exchange information using the localStorage. For example I could have the browser waiting for the variable x to exist. Once it exist I then creates a variable y by the browser notifying the c++ program that it is ready to receive data and so on. localStorage is just a sqlite database located on C:\\Users[USER]\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Local Storage

I haven't seen anyone online that uses this approach. Maybe it is too dangerous and Sqlite cannot handle multiple threads that good and I will be wasting time creating this program.

So should I start implementing this protocol? Should I use websockets? Or should I give it a try to https://stackoverflow.com/a/10219977/637142 ?

我会使用node.js作为从C ++到浏览器的中间件,而不是直接使用websocket(在那之前这样做了)与http://socket.io/一起使用,它将使您的生活更加轻松:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM