[英]Spring MVC : Implementing Cometd channel functionality
這是我已經實現的一些代碼,請看一下。
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
}
}
任何有關如何創建頻道,訂閱和通過其發送消息的幫助都將是不錯的。 如果有人要聊天,請發表評論,我將為此創建一個聊天室。 非常感謝。 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.