Yesterday I’ve discovered GWTEventService. Today i started to play around with it, after some research in the example-svn and reading the docs, I was able to write my own “Hello World”.
This Hello World is a small chatting application. You are only able to send messages but not set a nickname or something similar.
Here you can see what it will look like:
In this short overview I’ll just show you the parts that are associated with the GWTEventSerice. The whole regular GWT UI/RPC stuff is left out. If you want to see the complete example, you can download it here.
First of all you need an Event:
public class MyEvent implements Event {
public String message;
public MyEvent(){}
public MyEvent(String message) {
this.setMessage(message);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
This Event gets fired when the server gets a new message from a client.
The next thing you need is a EventListener that gets notified when an Event occures:
public class MyListener implements RemoteEventListener {
/**
* This function gets called by EventService
*/
public void apply(Event anEvent) {
/**
* Check if the incoming Event is from the type MyEvent and if so, call the corresponding function
*/
if(anEvent instanceof MyEvent)
onMyEvent((MyEvent)anEvent);
}
/**
* This function gets called when the incomming Event is from the Type MyEvent
*
* @param event
*/
public void onMyEvent(MyEvent event){}
}
The Listener receives the Events from the server an processes them.
Now on the client-side you need to register your Listener to the server:
/**
* The domain to which we want to register our listener to
*/
private static final Domain DOMAIN = DomainFactory.getDomain("my_domain");
public void onModuleLoad() {
/**
* Create an EventService
*/
RemoteEventServiceFactory theEventServiceFactory = RemoteEventServiceFactory.getInstance();
RemoteEventService theEventService = theEventServiceFactory.getRemoteEventService();
/**
* Register our listener to the domain
*/
theEventService.addListener(DOMAIN, new MyListener(){
public void onMyEvent(MyEvent event){
/**
* Get the message from the event and append it to the allready received messages
*/
getIncommingMessages().setText( getIncommingMessages().getText() + event.getMessage() + "n" );
}
});
[.....]
}
On the client-side we just register our Listener, and tell him what to do when an Event occurs.
The last thing that we need is the Server:
public class HelloWorldServiceImpl extends RemoteEventServiceServlet implements HelloWorldService {
/**
* The Domain that this Service sends Events to.
*/
private static final Domain DOMAIN = DomainFactory.getDomain("my_domain");
/**
* A normal function that gets called when a client sends a message to the server.
*/
public void sendMessage(String message) {
/**
* We dont save the incomming messages on the server. We just send an MyEvent to all
* registered Listeners on the Domain.
*/
this.addEvent(DOMAIN, new MyEvent(message));
}
}
Instead of the default RemoteServiceServlet we extend this one with RemoteEventServiceServlet from the GWTEventService lib. This gives you the ability to send Events to the clients.