Project

General

Profile

Wt is not refreshing a page appropriately

Added by Loren Burkholder over 2 years ago

Before I get started, I just want to say that Wt is pretty cool.

However, I have been playing around with writing a very simple demo chat application, and I have discovered that Wt is not updating my pages as it should be. The chat app is set up as follows: When a user types a message into the WLineEdit on the page and clicks the Send button, my app pushes the message, along with the sender's IP address for ID, to the end of an std::vector> that is stored in a database class. (I am using a global variable for this database. I know, I know, bad programming practice, but this is not supposed to be production code.) The database then emits a signal that contains the new message. Each WApplication instance is connected to this signal. The connection appends the message to a WText object displayed on the main page. The problem is that the only page that updates immediately is the page that sent the message. If I send a message from another page, that second page will load all previous messages as well as the just-sent message.

I cannot figure out why this is happening and I would be happy if somebody could shed a little light on the subject. The code is available at https://github.com/LorenDB/wt-playground.

Thanks in advance.


Replies (5)

RE: Wt is not refreshing a page appropriately - Added by Korneel Dumon over 2 years ago

Hi,

there is a example chat application in the folder simplechat. If your problem is specifically pushing updates from the server, this is a good explanation of what's necessary:
https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WApplication.html#ad9631ca64e68d30d40cb49c90e55223d

RE: Wt is not refreshing a page appropriately - Added by Loren Burkholder over 2 years ago

Thanks for the link. I decided that of the two updating methods described in the function description, the second one (locking the WApplication, applying updates, and then posting them with triggerUpdate()) seemed like the way to go; however, I can't get it to work. As for the first method, there isn't really any good documentation for it that I can find.

So I guess the question is, why doesn't triggerUpdate() actually update the page as it should? I have verified that I am calling triggerUpdate() on the correct WApplication instance, so it's not a case of duplicated calls on the same object.

RE: Wt is not refreshing a page appropriately - Added by Korneel Dumon over 2 years ago

You don't mention WApplication::enableUpdates(), did you set it to true? This has to be done during normal event handling, so in response to a user event. For example: user clicks 'join chat', in the click-handler you call enableUpdates(true) and from that point on you can push updates from the server to the client using one of the two methods. It's also good practice to call enableUpdates(false) when you no longer need it (for example when user exits the chat).

To further explain this, calling enableUpdates(true) tells the browser to do a long-polling request to the server. This request gives the server a connection over which to send updates. If you don't do this, the server has no way to contact the browser.

RE: Wt is not refreshing a page appropriately - Added by Loren Burkholder over 2 years ago

Yes, I'm calling enableUpdates(); unfortunately, even if I am calling it from the event loop, it doesn't work.

RE: Wt is not refreshing a page appropriately - Added by Loren Burkholder over 2 years ago

OK, I'm stupid.

I had been using a control flow that looked like this: modify page -> acquire lock -> triggerUpdate(). Changing this to acquire lock -> modify page -> triggerUpdate() solved the problem.

    (1-5/5)