HTTP Chat sample (Android)

This sample presents a small chat program allowing to send text messages over the local network or the Internet. The sample demonstrates how server callback events can be consumed by an application written in Java using the EventReceiver class.

Server Requirements

This sample can work together with the HTTP Chat Sample (Delphi) server or with the HTTP Chat Sample (.NET) server.

Getting Started

To test the sample you will need to start and activate the sample server for your preferred platform somewhere on the network. Then you can start two or more client applications on different platforms.
Check the AndroidHTTPChatActivity class to modify the following defined URL:

var lScheme := new URI("http://192.168.10.1:8099/bin");
URI lScheme = new URI("http://192.168.10.1:8099/bin");

Log in with different user names on different clients, try to post text messages to the common chat and see how they appear. All chat events are initiated from the user session on the server. You can try server-initiated events, there are two events of this kind supported. By clicking the corresponding button on the server form you can either warn all clients about an upcoming server shutdown or kick a user from the chat. From the client point of view there is no difference in how the events are initiated.

Examine the Code

Let's take a look how the event receiver is set up and how the client is being registered to receive particular events:

method AndroidHTTPChatActivity.initialize;
begin

  // Setting up receiver and service
  var lScheme := new URI("http://192.168.10.1:8099/bin"); 
  aChatService := new HTTPChatService_Proxy(lScheme);
  aChatReceiver := new EventReceiver(aChatService.ProxyMessage, ClientChannel.channelWithURI(lScheme));
  aChatReceiver.ServiceName := aChatService._getInterfaceName;
  aChatReceiver.MinimumPollInterval := 1;
  aChatReceiver.MaximumPollInterval := 3;
  // ...

  // Registering the client
  aLoginButton.OnClickListener := new interface View.OnClickListener(onClick :=
  method(v : View) begin
    var lUserLogin : String := aUsernameFiled.Text.toString;
    if assigned(lUserLogin) then
      try
        var lUserID : String := getService.Login(lUserLogin);
        if assigned(lUserID) then begin
          getReceiver.Active := true;
          var lIntent := new Intent().setClass(self, typeOf(ChatActivity));
          startActivityForResult(lIntent, 1);
        end
        else
          Toast.makeText(self, "Login failed", Toast.LENGTH_LONG).show;
      except
        on E:Exception do
          Log.e("AndroidHTTPChatActivity", "initialize: " + E.Message);
      end;
  end
  );
  // ...

end;
private void initialize() {

  // Setting up receiver and service
  URI lScheme = new URI("http://192.168.10.1:8099/bin");
  aChatService = new HTTPChatService_Proxy(lScheme);
  aChatReceiver = new EventReceiver(aChatService.getProxyMessage(), ClientChannel.channelWithURI(lScheme));
  aChatReceiver.setServiceName(aChatService._getInterfaceName());
  aChatReceiver.setMinimumPollInterval(1);
  aChatReceiver.setMaximumPollInterval(3);
  // ...

  // Registering the client
  aLoginButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      String lUserLogin = aUsernameFiled.getText().toString();
      if (( lUserLogin != null )) {
        try {
          String lUserID = getService().Login(lUserLogin);
          if (( lUserID != null )) {
            getReceiver().setActive(true);
            Intent lIntent = new Intent().setClass(AndroidHTTPChatActivity.this, ChatActivity.class);
            startActivityForResult(lIntent, 1);
          } else
            Toast.makeText(AndroidHTTPChatActivity.this, "Login failed", Toast.LENGTH_LONG).show();
        } catch(Exception e) {
          Log.e("AndroidHTTPChatActivity", "initialize: " + e.getMessage());
        }
      }
    }
  });
  // ...

}

The unregistering and stopping action:

aLogoutButton.OnClickListener := new interface View.OnClickListener(onClick :=
method(v : View);
begin
  AndroidHTTPChatActivity.getService.Logout;
  var lIntent : Intent := Intent;
  setResult(Activity.RESULT_OK, lIntent);
  finish;
end);
aLogoutButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    AndroidHTTPChatActivity.getService().Logout();
    Intent lIntent = getIntent();
    setResult(Activity.RESULT_OK, lIntent);
    finish();
  }
});     

Let's take a look at the event handling methods:

Notifier := EventNotifier.eventNotifierInstance;
Notifier.addEventListener(new interface IROEvents(OnLogin :=
  method(aEvent : OnLoginEvent) begin
  // ...
  end));
Notifier.addEventListener(new interface IROEvents(OnLogout :=
  method(aEvent : OnLogoutEvent) begin
  // ...
  end));
Notifier.addEventListener(new interface IROEvents(OnSendMessage :=
  method(aEvent : OnSendMessageEvent) begin
  // ...
  end));
Notifier.addEventListener(new interface IROEvents(OnSystemShutdown :=
  method(aEvent : OnSystemShutdownEvent) begin
  // ...
  end));
Notifier.addEventListener(new interface IROEvents(OnMandatoryClose :=
  method(aEvent : OnMandatoryCloseEvent) begin
  // ...
  end));
Notifier.addEventListener(new interface IROEvents(OnException :=
  method(aEvent : OnExceptionEvent) begin
  // ...
  end));
Notifier = EventNotifier.eventNotifierInstance();
Notifier.addEventListener(new EventsAdapter() {
  @Override
  public void OnLogin(OnLoginEvent aEvent) {
  // ...
  });
Notifier.addEventListener(new EventsAdapter() {
  @Override
  public void OnLogout(OnLogoutEvent aEvent) {
  // ...
  });
Notifier.addEventListener(new EventsAdapter() {
  @Override
  public void OnSendMessage(OnSendMessageEvent aEvent) {
  // ...
  });
Notifier.addEventListener(new EventsAdapter() {
  @Override
  public void OnSystemShutdown(OnSystemShutdownEvent aEvent) {
  // ...
  });
Notifier.addEventListener(new EventsAdapter() {
  @Override
  public void OnMandatoryClose(OnMandatoryCloseEvent aEvent) {
  // ...
  });
Notifier.addEventListener(new EventsAdapter() {
  @Override
  public void OnException(OnExceptionEvent aEvent) {
  // ...
  });