Springboot集成Websocket
1.导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.编写Websocket配置类
@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpoint(){
return new ServerEndpointExporter();
}
}
3.编写Controller类
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@Slf4j
@Component
@ServerEndpoint("/websocket")
public class WebSocketController {
@OnOpen
public void onOpen(Session session) {
log.info("客户端:" + session.getId() + "连接成功");
}
@OnClose
public void onClose(Session session) {
log.info("客户端:" + session.getId() + "连接断开");
}
@OnMessage
public String onMsg(String message, Session session) {
log.info("从客户端:" + session.getId() + " 收到消息:" + message);
return "返回消息:" + message;
}
}
4.测试
http://www.websocket-test.com/
https://www.dute.org/websocket
标签:websocket,Springboot,16,javax,session,Session,import,Websocket,public
From: https://www.cnblogs.com/NIAN2011/p/16816176.html