0

I am currently developing a C# application that pulls information from a specific program. The information is pulled and updated about once every second. I want to export the information to a locally stored website (with HTML, CSS, and JS), and have it updated constantly without any need of refreshing the page.

Is there any way to do this?

The information stored will be in a string format, if it helps.

CAG2
  • 381
  • 2
  • 14
  • Have you write some code for that purpose if so then post it here......... – Ammar Ali Aug 29 '18 at 04:17
  • No, because I am out of ideas. I want to know if there is a way to do it, and find out how to execute that method. – CAG2 Aug 29 '18 at 04:24
  • Can this local website read a file? If so why not put the string there? Otherwise use a database or add a POST route where the C# app can post the data. – Cheesebaron Aug 29 '18 at 04:39
  • For security measures, most browsers disallow the reading of local files. Is it possible to set up some form of local/offline database or similar? I'm more of a solo front-end developer, so I don't know how any of the server-side stuff works – CAG2 Aug 29 '18 at 06:27

1 Answers1

0

You will need to have a server read in the data and serve it on some path. The client webpage can make AJAX requests at timed intervals to get the data from the server.

If you have a domain name like http://example.com and you serve the data from /dataPath, then the client-side javascript that pulls data from the server every second could look something like this:

(function timedFunction () {
  setTimeout(function getData () {
    fetch('http://example.com/dataPath')
      .then(function(response) {
        // Do something with data
      });
    timedFunction();
  }, 1000);
})();
Christopher Bradshaw
  • 2,615
  • 4
  • 24
  • 38
  • The problem is, I don't have any form of domain. Is there any way to do this without a domain? – CAG2 Aug 29 '18 at 12:07
  • Yeah, no worries! Just use the IP address of the place you're hosting it from in place of the domain name. – Christopher Bradshaw Aug 29 '18 at 18:13
  • If this helped you, I'd appreciate you marking it as the accepted answer. If there's anything you need, please let me know so I can update my answer. – Christopher Bradshaw Aug 29 '18 at 19:08
  • Thanks, I'll do that. One more question - this will also work on others' computers as well as my own, right? I assume I'd use localhost for that – CAG2 Aug 30 '18 at 00:49
  • Anyone should be able to access the website from the web server's public ip address. You should run the webserver on port 80 (assuming it's http, not https) on localhost or have the port it runs on forwarded to port 80. Then another computer would access it on `http://[PUBLIC_IP_ADDRESS]/dataPath`. – Christopher Bradshaw Aug 30 '18 at 04:04
  • What if I want to run the server using localhost (I want the person using the application to host the server on their internet, and they should be the only one able to access it)? – CAG2 Aug 31 '18 at 02:15
  • If you only want the person running the server to access it, then you would probably just use a firewall to block incoming traffic on that port. – Christopher Bradshaw Aug 31 '18 at 14:30
  • Can the Javascript code you posted above read data from a simple localhost website that doesn't have any form of database? In Chrome, I get this error: "Failed to load http://localhost/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled." – CAG2 Sep 06 '18 at 04:58
  • There are several questions on StackOverflow dealing with the same error. I'd recommend googling your error message and looking through the accepted answers there. For this particular issue, [this](https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header) thread might help. The error basically means you need to enable cors on your server. It's not a client-side issue or an issue with the javascript snippet I posted. – Christopher Bradshaw Sep 06 '18 at 17:21