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
var lURI := new URI(String(textField3.Text));
// Setting up the login service, chat service and event receiver
fLoginService := new LoginService_Proxy(lURI);
fChatService := new HTTPChatService_Proxy(fLoginService.ProxyMessage, fLoginService.ProxyClientChannel);
fChatReceiver := new EventReceiver(fLoginService, "HTTPChat");
fChatReceiver.AdjustPollInterval := false;
fChatReceiver.PollInterval := 500;
// ... attaching the event listeners (see below) ...
// Registering the client
var lUserName := textField1.Text;
var lUserID: String := LoginService.Login(lUserName);
if assigned(lUserID) then
ChatReceiver.start;
end));
private void button1MouseClicked(MouseEvent e) {
URI lURI = null;
try {
lURI = new URI((String) textField3.getText());
} catch (URISyntaxException e1) {
System.err.println("ChatWindow : Throws URISyntaxException");
}
// Setting up the login service, chat service and event receiver
LoginService = new LoginService_Proxy(lURI);
ChatService = new HTTPChatService_Proxy(LoginService.getProxyMessage(), LoginService.getProxyClientChannel());
ChatReceiver = new EventReceiver(LoginService, "HTTPChat");
ChatReceiver.setAdjustPollInterval(false);
ChatReceiver.setMinPollInterval(500);
// ... attaching the event listeners (see below) ...
// Registering the client
String lUserName = textField1.getText();
String lUserID = LoginService.Login(lUserName);
if (lUserID != null) {
ChatReceiver.start();
LoggedUsersList();
button1.setEnabled(false);
button2.setEnabled(true);
button3.setEnabled(true);
}
}
The unregistering and stopping action:
button2.addMouseListener(new interface MouseListener(mouseClicked :=
method(e: MouseEvent) begin
LoginService.Logout;
var lModel := new DefaultListModel;
list1.Model := lModel;
ChatReceiver.stop;
button1.Enabled := true;
button2.Enabled := false;
button3.Enabled := false;
end));
private void button2MouseClicked(MouseEvent e) {
LoginService.Logout();
DefaultListModel lModel = new DefaultListModel();
list1.setModel(lModel);
ChatReceiver.stop();
button1.setEnabled(true);
button2.setEnabled(false);
button3.setEnabled(false);
}
Let's take a look at the event handling methods:
fChatReceiver.addListener(new interface IHTTPChatEvents(OnLogin :=
method(aEvent : OnLoginEvent) begin
// ...
end));
fChatReceiver.addListener(new interface IHTTPChatEvents(OnLogout :=
method(aEvent : OnLogoutEvent) begin
// ...
end));
fChatReceiver.addListener(new interface IHTTPChatEvents(OnSendMessage :=
method(aEvent : OnSendMessageEvent) begin
// ...
end));
fChatReceiver.addListener(new interface IHTTPChatServerEvents(OnSystemShutdown :=
method(aEvent : OnSystemShutdownEvent) begin
// ...
end));
fChatReceiver.addListener(new interface IHTTPChatServerEvents(OnMandatoryClose :=
method(aEvent : OnMandatoryCloseEvent) begin
// ...
end));
ChatReceiver.addListener(new IHTTPChatEvents_Adapter() {
@Override
public void OnLogin(OnLoginEvent aEvent) {
// ...
}
});
ChatReceiver.addListener(new IHTTPChatEvents_Adapter() {
@Override
public void OnLogout(OnLogoutEvent aEvent) {
// ...
}
});
ChatReceiver.addListener(new IHTTPChatEvents_Adapter() {
@Override
public void OnSendMessage(OnSendMessageEvent aEvent) {
// ...
}
});
ChatReceiver.addListener(new IHTTPChatServerEvents_Adapter() {
@Override
public void OnSystemShutdown(OnSystemShutdownEvent aEvent) {
// ...
}
});
ChatReceiver.addListener(new IHTTPChatServerEvents_Adapter() {
@Override
public void OnMandatoryClose(OnMandatoryCloseEvent aEvent) {
// ...
}
});