我在spring boot中构建项目,我想在我的项目中实现聊天应用程序逻辑我正在使用websockets,这是我的代码
@SpringBootApplication
public class SamepinchApplication extends SpringBootServletInitializer{
@Bean
public EchoEndpoint echoEndpoint() {
return new EchoEndpoint();
}
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
@Bean
public ServletContextAware endpointExporterInitializer(final ApplicationContext applicationContext) {
return new ServletContextAware() {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SamepinchApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SamepinchApplication.class, args);
}
}
和EchoEndpoint
@ServerEndpoint(value = "/api/v1/chat/{username}")
public class EchoEndpoint {
private static final Logger log = LoggerFactory.getLogger(EchoEndpoint.class);
private static final Set<Session> sessionsSet = Collections.synchronizedSet(new HashSet<Session>());
private static final ConcurrentHashMap<String, Object> sessions = new ConcurrentHashMap<String, Object>();
@OnOpen
public void onWebSocketConnect(Session session, @PathParam("username") String uname) throws IOException {
session.getBasicRemote().sendText("Connection established with session id "+session.getId());
sessionsSet.add(session);
sessions.put(uname, session);
System.out.println("Connected with: " + session.getId());
log.info("Connected with: " + session.getId());
}
@OnMessage
public void onWebSocketMessage(Session session, String message) throws IOException {
System.out.println("getting message......");
System.out.println(message.substring(message.indexOf(":",0)+1));
String[] mesgArray = message.split(":");
System.out.println(mesgArray[0]);
Session sess = (Session) sessions.get(mesgArray[0]);
if(sess != null)
sess.getBasicRemote().sendText(message.substring(message.indexOf(":",0)+1));
// sendMessageToAll(message);
}
@OnClose
public void onWebSocketClose(Session session) {
System.out.println("Closed: " + session.getId());
log.info("Closed: " + session.getId());
}
@OnError
public void onWebSocketError(Session session, Throwable error) {
System.out.println("Error (from " + session.getId() + "): " + error.getMessage());
log.info("Error (from " + session.getId() + "): " + error.getMessage());
}
private void sendMessageToAll(String message){
for(Session s : sessionsSet){
try {
log.info("send from qasim siddiqui");
log.info(s.getId());
s.getBasicRemote().sendText(message);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
当我在ecllipse中运行代码时,这段代码运行正常但是当我将它部署到tomcat时它给了我这个异常
SEVERE: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/samepinch-0.0.1-SNAPSHOT]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Failed to register @ServerEndpoint class: class com.samepinch.socket.EchoEndpoint
at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:148)
at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoints(ServerEndpointExporter.java:129)
at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterSingletonsInstantiated(ServerEndpointExporter.java:107)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:117)
at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:108)
at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:68)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Caused by: javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/api/v1/chat/{username}]
at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:199)
at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:271)
at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:145)
... 24 more
在tomcat上它尝试创建多个端点,我没有得到任何解决方案。任何人都可以帮助我!
我正在使用websocket的这个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
答案 0 :(得分:1)
你删除
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
Tomcat不需要这个
答案 1 :(得分:0)
您正在生产中创建多个端点,您应该在开发中需要回波端点而不是生产中..根据环境进行配置
if (Environment.current == Environment.DEVELOPMENT) {
// then create echo point
}