站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Eclipse Rich Client Platform: Designing, Coding, and Packaging Java Applications

Section 10.4.  Chatting with Eliza - Eclipse Rich Client Platform: Designing, Coding, and Packaging Java Applications

Previous Page
Next Page

10.4. Chatting with Eliza

Now that Hyperbola has a real messaging library behind it, exercising it gets a little more tricky. You need to have a real messaging server and someone to chat with. When you tested the bundled Smack, you connected to eclipsercp.org as "reader" and then chatted with eliza@eclipsercp.org, a robot chat agent. Let's do the same thing with Hyperbola.

Open the Application class and modify run(Object) to call a login method before starting the Workbench as shown in the snippet below.

org.eclipsercp.hyperbola/Application
public class Application implements IPlatformRunnable {
  public Object run(Object args) throws Exception {
    Display display = PlatformUI.createDisplay();
    try {
      final Session session = Session.getInstance();
      if (!login(session))
        return IPlatformRunnable.EXIT_OK;
      int returnCode =
          PlatformUI.createAndRunWorkbench(display,
              new ApplicationWorkbenchAdvisor());
      if (returnCode == PlatformUI.RETURN_RESTART)
        return IPlatformRunnable.EXIT_RESTART;
      return IPlatformRunnable.EXIT_OK;
    } finally {
      display.dispose();
    }
  }

  private boolean login(Session session) {
    try {
      ConnectionDetails d =
          new ConnectionDetails("reader", "eclipsercp.org", "secret");
      XMPPConnection con = new XMPPConnection(d.getServer());
      con.login(d.getUserId(), d.getPassword(), d.getResource());
      session.setConnection(con);
      session.setConnectionDetails(d);
    } catch (XMPPException e) {
      return false;
    }
    return true;
  }
}

Add the login(Session) method as shown. This is basically the same code from when you were testing earlier. The code opens the connection, stashes it and the connection details in the given session, and returns. If the login fails, false is returned and Hyperbola exits.

Now start the Hyperbola application. You are automatically logged onto the server as "reader." Hyperbola shows you the contacts list that contains only one entry, "eliza." Select that entry and use Hyperbola > Chat to start chatting with Eliza, as shown in Figure 10-3.

Figure 10-3. Chatting with Eliza


That's it. The Hyperbola application is now a functioning chat client.


Previous Page
Next Page