websocket
This commit is contained in:
parent
bec6661aa0
commit
7f22375f93
Binary file not shown.
4
pom.xml
4
pom.xml
|
|
@ -82,6 +82,10 @@
|
|||
<version>2.2.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
// 不进行权限验证的请求或资源(从配置文件中读取)
|
||||
.antMatchers("/", "/login/**","/favicon.ico").permitAll()
|
||||
.antMatchers("/", "/login/**","/favicon.ico","/websocket/**","/ws/**").permitAll()
|
||||
// 其他的需要登陆后才能访问
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package com.bonus.aqd.websocket.config;
|
||||
|
||||
import com.bonus.aqd.websocket.handle.AqdSocketHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
|
||||
|
||||
/**
|
||||
* <h2></h2>
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author LiYang
|
||||
* @createTime 2022年07月19日 9:57 上午
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
public class WebSocketConfig implements WebSocketConfigurer {
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
// 注册 socket处理器
|
||||
registry.addHandler(AqdSocketHandler (), "/ws/aqd").setAllowedOrigins("*");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletServerContainerFactoryBean createWebSocketContainer() {
|
||||
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
|
||||
// 消息缓冲区大小的大小
|
||||
container.setMaxTextMessageBufferSize(8192);
|
||||
container.setMaxBinaryMessageBufferSize(8192);
|
||||
return container;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public WebSocketHandler AqdSocketHandler () {
|
||||
return new AqdSocketHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package com.bonus.aqd.websocket.handle;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.socket.*;
|
||||
|
||||
|
||||
/**
|
||||
* 安全带文本socket
|
||||
* @author 黑子
|
||||
*/
|
||||
public class AqdSocketHandler implements org.springframework.web.socket.WebSocketHandler {
|
||||
|
||||
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(AqdSocketHandler.class);
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
LOGGER.info("Socket 连接成功,sessionId:{}", session.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
||||
if (message instanceof TextMessage) {
|
||||
handlerTextMessage(session, (TextMessage) message);
|
||||
} else {
|
||||
LOGGER.error(" Socket 消息处理失败,只接受 文本消息,sessionId:{}", session.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void handlerTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
final String msg = message.getPayload();
|
||||
LOGGER.info(" Socket 收到消息:{}", msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||
LOGGER.error(" Socket 处理异常,sessionId:{}, 异常原因:{}", session.getId(), exception.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
||||
LOGGER.info(" Socket 关闭,sessionId:{}", session.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsPartialMessages() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue