我正在使用Spring-MVC应用程序,我希望将个性化聊天作为一项功能包含在其中。经过一些研究,我发现Cometd是一个合适的选择。在完成文档并永远重复样本之后,我有一些我已经完成的设置。我需要一些帮助来在spring-mvc应用程序中集成个性化聊天服务,并在用户按下聊天按钮时启用私人聊天。
所以基本上,我发现了," / service / chat"可以用于私人聊天,所以我有一个类,并使用私人聊天,我必须有userid< - > sessionId的映射,但我找不到任何地方的例子如何做到这一点。我发布了一些我的代码,请让我知道剩下要做的事情,如果可能的话,还有一些资源,样本。
控制器代码:
@Controller
@Singleton
public class MessageController {
private MessageService messageService;
@Autowired(required = true)
@Qualifier(value ="messageService")
public void setMessageService(MessageService messageService){this.messageService=messageService;}
@RequestMapping(value = "/startchatting", produces = "application/text")
@ResponseBody
public String startChattingService(){
return "OK";
}
@RequestMapping(value = "/stopchatting",produces = "application/text")
@ResponseBody
public String stopChatting(){
return "OK";
}
}
私信消息服务:
@Service
public class PrivateMessageService {
@Session
private ServerSession session;
@Listener("/service/private")
public void handlePrivateMessage(ServerSession sender, ServerMessage message){
String userId = (String) message.get("targetUserId");
//Mapping code necessary to map userids to session-id's.
//ServerSession recipient = findServerSessionFromUserId(userId);
//recipient.deliver(session,message.getChannel(),message.getData(),null);
}
}
CometConfigurer:
@Component
@Singleton
public class CometConfigurer {
private BayeuxServer bayeuxServer;
private ServerAnnotationProcessor processor;
@Inject
public void setBayeuxServer(BayeuxServer bayeuxServer){this.bayeuxServer = bayeuxServer;}
@PostConstruct
public void init() {this.processor= new ServerAnnotationProcessor(bayeuxServer);}
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
System.out.println("Configuring service " + name);
processor.processDependencies(bean);
processor.processConfigurations(bean);
processor.processCallbacks(bean);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
return bean;
}
public void postProcessBeforeDestruction(Object bean, String name) throws BeansException {
processor.deprocessCallbacks(bean);
}
@Bean(initMethod = "start", destroyMethod = "stop")
public BayeuxServer bayeuxServer() {
BayeuxServerImpl bean = new BayeuxServerImpl();
// bean.setOption(BayeuxServerImpl.LOG_LEVEL, "3");
return bean;
}
public void setServletContext(ServletContext servletContext) {
servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
}
}
Cometd beans:
<beans:bean id="bayeuxServer" class="org.cometd.server.BayeuxServerImpl" init-method="start" destroy-method="stop"/>
我直接包含了来自https://github.com/fredang/cometd-spring-example的配置和设置的JSP文件,并对其进行了修改以满足我的需求。请告诉我还剩下什么,欢迎所有建议,我无法在网上找到相同任务的任何示例,详细内容,并有更多代码然后解释。谢谢。
答案 0 :(得分:1)
使用Spring 4.x的新WebSocket功能肯定会起作用;此外,这个新模块为您的用例提供了许多非常有趣的功能:
您可以查看this nice chat application that demonstrates all those features。