HTTP Chat sample (Java)

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.
Set the server address in the client form and log in with different user names on different clients, try to post text messages to the common chat and see how they appear. Try to post private messages. To post a private message to a user selected from the user list by left clicking on their name (you can select more users if you hold down the ctrl button while clicking the mouse button). 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 users from the chat. From the client point of view there is no difference in how the events are initiated.

Examine the Code

All code parts that require attention are located in the UserInterface.pas file for Oxygene or the ChatWindow.java file for Java, respectively. Let's take a look at how the event receiver is set up and how the client is being registered to receive particular events:

button1.addMouseListener(new interface MouseListener(MouseClicked :=
method(e: MouseEvent) begin

  // Setting up receiver and service
  var lScheme := new URI(String(textField3.Text));
  fChatService := new HTTPChatService_Proxy(lScheme);
  fChatReceiver := new EventReceiver(fChatService.ProxyMessage, ClientChannel.channelWithURI(lScheme)); 
  fChatReceiver.ServiceName := fChatService._getInterfaceName;
  fChatReceiver.MinimumPollInterval := 1;
  fChatReceiver.MaximumPollInterval := 3;
  var lUserName := textField1.Text;

  // Registering the client
  var lUserID : String := ChatService.Login(lUserName);
  if assigned(lUserID) then
    ChatReceiver.Active := true;
end));
private void button1MouseClicked(MouseEvent e) {

  // Setting up receiver and service
  URI lScheme = new URI((String) textField3.getText());
  ChatService = new HTTPChatService_Proxy(lScheme);
  ChatReceiver = new EventReceiver(ChatService.getProxyMessage(),
  ClientChannel.channelWithURI(lScheme));
  ChatReceiver.setServiceName(ChatService._getInterfaceName());
  ChatReceiver.setMinimumPollInterval(1);
  ChatReceiver.setMaximumPollInterval(3);
  String lUserName = textField1.getText();
  
  // Registering the client
  String lUserID = ChatService.Login(lUserName);
  if (lUserID != null)
    ChatReceiver.setActive(true);
}

The unregistering and stopping action:

button2.addMouseListener(new interface MouseListener(MouseClicked :=
  method(e: MouseEvent);
  begin
    ChatService.Logout;
    var lModel := new DefaultListModel;
    list1.Model := lModel;
    ChatReceiver.Active := false;
    // ...
  end));
private void button2MouseClicked(MouseEvent e) {
  ChatService.Logout();
  DefaultListModel lModel = new DefaultListModel();
  list1.setModel(lModel);
  ChatReceiver.setActive(false);
  // ...
}

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) {
  // ...
  });