سؤال

I am creating an application and was told that it would be best to use Comet for what I want. I've had no experience with Comet until now, but after a little research, I have set up a CometD server. Other servers available included APE, but it seemed a while since it's last revision, and the documentation for CometD was much better.

Due to my lack of experience I don't really know where to begin, and the documentation doesn't cover a case as specific as this.

I want the server to continuously watch a .txt file for changes, and display the current contents of that document in an HTML page. Periodically, that file changes (roughly every 2 or 3 minutes), and I want the new document to be displayed to the connected clients when that change occurs.

I presume it is doable, and I imagine it can be done solely by modifying the JS. My only question is... how do I start?

هل كانت مفيدة؟

المحلول

You can use JDK 7's file watcher service to watch for file changes, as described in the official tutorial.

One you detect a change, you can broadcast a CometD message on a channel, and JavaScript clients can listen for messages on that channel. See section 3.3.5.5 of http://docs.cometd.org.

You can choose to send the whole file or deltas, depending on your application logic.

To sum up, you need some logic on the server side to watch for file changes, and some logic on the client to receive the file changes and display them.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top