- I am working on a Spring-MVC application in which I would like to implement chat functionality provided by Cometd. Now, in this project, there are various groups each with unique-id(Primary key). Whenever a group is created I would like to create a channel and then when a member sends a message inside the group, then the message is sent through that channel so it goes to the groupMembers only. No persistence of chat messages is required,just that messages should be pushed to all those who are online.
- My main motivation behind selecting cometd was as I read there is a lot of documentation and help, but as I see, there are hardly good examples on net and the documentation on the project website is obscured with too much technical details. I have implemented some code, but to be honest, I don't know what to do next and where to go next.
Here is some of the code I have implemented, kindly have a look.
ChatService.java :
package com.journaldev.spring.chat;
import org.cometd.annotation.ServerAnnotationProcessor;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.server.BayeuxServerImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.ServletContext;
@Component
@Singleton
public class ChatService{
private BayeuxServer bayeuxServer;
private ServerAnnotationProcessor serverAnnotationProcessor;
@Inject
public void setBayeuxServer(BayeuxServer bayeuxServer){
this.bayeuxServer = bayeuxServer;
}
@PostConstruct
public void init(){
this.serverAnnotationProcessor = new ServerAnnotationProcessor(bayeuxServer);
}
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException{
System.out.println("Configuring Service "+name);
serverAnnotationProcessor.processDependencies(bean);
serverAnnotationProcessor.processConfigurations(bean);
serverAnnotationProcessor.processCallbacks(bean);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
return bean;
}
public void postProcessBeforeDestruction(Object bean, String name) throws BeansException {
serverAnnotationProcessor.deprocessCallbacks(bean);
}
@Bean(initMethod = "start", destroyMethod = "stop")
public BayeuxServer bayeuxServer() {
// bean.setOption(BayeuxServerImpl.LOG, "3");
return new BayeuxServerImpl();
}
public void setServletContext(ServletContext servletContext) {
servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
}
}
ChatProducer :
public class ChatProducer {
private ChatService chatService;
public void setChatService(ChatService chatService){
this.chatService = chatService;
}
public synchronized void sendMessages(String messages){
// Unfortunately I don't know where to send the messsages and how
}
}
Any help would be nice as to how to create channels, subscribe and send messages over them. If anyone wants to chat for this, kindly post a comment, I will create a chatroom for the same. Thanks a lot. :-)
Aucun commentaire:
Enregistrer un commentaire