Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Wt example 4: broadcasting using WtBroadcastServer

 

Wt example 4: broadcasting using WtBroadcastServera is an article about a simple Wt example in which is shown how multiple clients can respond to the same data source and be notified when needed.

 

This example shows how to use two classes to do most of the work for you. For a complete view of all delegated tasks, Wt example 3: broadcasting gives a complete view of all bookkeeping necessary.

 

The code shown is simplified from the tool TestBroadcastServer (version 1.0).

 

This example has the following players:

 

The program does the following:

 

Below, the WtMainDialog and its communication to the WtBroadcastServer is discussed in detail, starting at its header file.

 

 

 

 

The WtBroadcastServer header file

 

struct WtMainDialog : public Wt::WContainerWidget, WtBroadcastServerClient
{
  WtMainDialog();

  private:
  ///The user interface
  struct Ui
  {
    Ui() : m_edit(0) {}
    Wt::WLineEdit * m_edit;
  } ui;

  ///The user changes the text in the Wt::WLineEdit
  void OnEditChanged();

  ///The server updates the page
  void UpdatePage();
};

 

This header file has the following elements:

 

 

 

 

 

The WtBroadcastServer implementation file

 

WtMainDialog::WtMainDialog()
{
  ui.m_edit = new Wt::WLineEdit(this);
  ui.m_edit->keyWentUp().connect(this,&WtMainDialog::OnEditChanged);
}
//---------------------------------------------------------------------------
void WtMainDialog::OnEditChanged()
{
  WtBroadcastServer::GetInstance()->SetData(
    std::string(ui.m_edit->text().toUTF8()));
}
//---------------------------------------------------------------------------
void WtMainDialog::UpdatePage()
{
  std::string text;
  try
  {
    text = boost::any_cast<std::string>(WtBroadcastServer::GetInstance()->GetData());
  }
  catch (boost::bad_any_cast&)
  {
    text = "TestBroadcastServer";
  }
  ui.m_edit->setText(text.c_str());
}

 

WtMainDialog has only three methods:

 

 

 

 

 

Conclusion

 

Compared to the previous example, that is Wt example 3: broadcasting, this example is shorter and, in my humble opinion, better focussed: all bookkeeping is done by WtBroadcastServer and WtBroadcastServerClient.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict