Tuesday, December 11, 2007

Free Chat Implementation in GWT

I want to share my chat implementation for GWT, this is a pretty simple code which demonstrate how to write an Instand Messaging system inside the browser using AJAX and GWT technology.

The idea is simple, we have a chat factory in which each client can subscribe, There is a Chat Panel in which the user can send messages to the sever in async way. In the server there is a List which holds all the messages which all the clients sent.

There is a polling mechanism which run every few seconds and check if there are new chat messages to display.

The implementation is not state of the art but it works and it can be improved to work better.

Enjoy the implementation:


public class MyChatFactory {

private static final MyChatFactory instance = new MyChatFactory();
private static final int DELAY = 10000;

private MyChatFactory() {
}

public static MyChatFactory getInstance() {
return instance;
}

public void subscribeToEvent(final String queueName, final ChatCallback callback) {
ClientUtil.log("start subscribeToEvent");

Timer eventsTimer = new Timer() {
public void run() {
ChatProxy.getEvents(queueName, callback);
schedule(DELAY);
}
};

eventsTimer.schedule(DELAY);
}

public void sendMessage(String queueName, ChatMessage message) {
ChatProxy.sendMessage(queueName, message);
}

public void sendMessageToAll(ChatMessage message) {
ChatProxy.sendMessage(null, message);
}
}

public class ChatCallback extends GeneralCallback {

public ChatCallback() {
}

public void onSuccess(Object result) {
//ClientUtil.log("start ChatCallback.onSuccess");
if (result!=null) {
ChatMessage chatMessage = (ChatMessage) result;

if (chatMessage.isLogin()) {
ChatPanel.getInstance().addUser(chatMessage.getMessage());
} else {
ChatPanel.getInstance().addText(chatMessage.getMessage());
}
}
//ClientUtil.log("finish ChatCallback.onSuccess");
}

public void onFailure(Throwable caught) {
ClientUtil.log(caught.getMessage());
}
}

public class ChatMessage implements IsSerializable{

private String message;
private boolean isLogin;

public ChatMessage() {
}

public ChatMessage(String message, boolean isLogin) {
this.message=message;
this.isLogin=isLogin;
}


public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean isLogin) {
this.isLogin = isLogin;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

public class ChatProxy {

private static ChatServiceAsync chatService = (ChatServiceAsync) GWT.create(ChatService.class);

static {
ClientUtil.setEndpointForService(chatService, "chat");
}

public static void getEvents(String queueName, ChatCallback callback) {
chatService.getEvents(queueName, callback);

}

public static void sendMessage(String queueName, ChatMessage message) {
VoidCallback callback = new VoidCallback();
chatService.sendMessage(queueName, message, callback);
}

}

public interface ChatService extends RemoteService {

public ChatMessage getEvents(String queueName);

public void sendMessage(String queueName, ChatMessage chatMessage);
}

public class ChatServiceImpl extends RemoteServiceServlet implements ChatService {

private static final long serialVersionUID = 1L;

private static Map subscribers = new HashMap();
//private static final long INTERVAL = 200000-1000;

public ChatMessage getEvents(String queueName) {
List list = null;

if (!subscribers.containsKey(queueName)) {
list = new ArrayList();
subscribers.put(queueName, list);
} else {
list = (List) subscribers.get(queueName);
}

if (!list.isEmpty()) {
ChatMessage message = (ChatMessage) list.get(0);
list.remove(0);
return message;
}

return null;
}

public void sendMessage(String queueName, ChatMessage chatMessage) {

if (queueName!=null) {
if (subscribers.containsKey(queueName)) {
List list = (List) subscribers.get(queueName);
list.add(chatMessage);
}
} else { // If null then send to all
Iterator subscribeIter = subscribers.values().iterator();

while (subscribeIter.hasNext()) {
List list = (List) subscribeIter.next();
list.add(chatMessage);
// list.notify();
}
}
}

}

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Links

 
RSS Feeds Submission Directory