WebSocketDemo
暂无消息
package com.demo.websocket.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite;
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 com.demo.websocket.handler.CustomWebSocketHandler;
import com.demo.websocket.intercepter.WebSocketHandshakeIntercept;
@Configuration
@EnableWebSocket
@EnableWebMvc
public class WebSocketConfig implements WebSocketConfigurer {
/**
* 将 WebSocket 处理器添加到注册中心
*/
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
webSocketHandlerRegistry.addHandler(getWebSocketHandler(), "/websocket")
.addInterceptors(new WebSocketHandshakeIntercept()).setAllowedOrigins("*");
}
/**
* 获取WebSocket处理器,可以根据业务需要自定义处理器
*/
@Bean
public WebSocketHandler getWebSocketHandler() {
return new CustomWebSocketHandler();
}
/**
* 调整处理异常的优先级
*/
@Bean
public HandlerExceptionResolver handlerExceptionResolver() {
List exceptionResolvers = new ArrayList();
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
composite.setOrder(10);
composite.setExceptionResolvers(exceptionResolvers);
return composite;
}
}
package com.demo.websocket.intercepter;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import com.demo.websocket.util.WebSocketSessionUtils;
public class WebSocketHandshakeIntercept implements HandshakeInterceptor {
/**
* 设置sessionId
*/
@Override
public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse,
WebSocketHandler webSocketHandler, Map attributes) throws Exception {
HttpSession session = getSession(serverHttpRequest);
if (session != null) {
String sessionId = (String) session.getAttribute("sessionId");
if (sessionId == null) {
sessionId = WebSocketSessionUtils.getUUID();
}
attributes.put("sessionId", sessionId);
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse,
WebSocketHandler webSocketHandler, Exception exception) {
}
private HttpSession getSession(ServerHttpRequest serverHttpRequest) {
if (serverHttpRequest instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) serverHttpRequest;
return servletServerHttpRequest.getServletRequest().getSession(true);
}
return null;
}
}
package com.demo.websocket.handler;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections.MapUtils;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import com.demo.websocket.util.WebSocketSessionUtils;
public class CustomWebSocketHandler implements WebSocketHandler {
/**
* 建立连接之后的操作
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
String sessionId = MapUtils.getString(session.getAttributes(), "sessionId");
WebSocketSessionUtils.add(sessionId, session);
}
/**
* 异常处理
*/
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
String sessionId = MapUtils.getString(session.getAttributes(), "sessionId");
WebSocketSessionUtils.remove(sessionId);
}
/**
* 关闭连接之后的操作
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
String sessionId = MapUtils.getString(session.getAttributes(), "sessionId");
WebSocketSessionUtils.remove(sessionId);
}
/**
* 处理消息
*/
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage> message) throws Exception {
if (message instanceof TextMessage) {
// 获取前端发送的消息
String msg = message.getPayload().toString();
System.out.println("已收到消息:" + msg);
String sessionId = MapUtils.getString(session.getAttributes(), "sessionId");
// 等待10s,模拟后台处理消息
TimeUnit.SECONDS.sleep(10);
if (WebSocketSessionUtils.exists(sessionId)) {
// 如果存在此sessionId,则向其发送应答消息
WebSocketSessionUtils.sendMessage(sessionId, "hello client");
}
}
}
/**
* 是否处理分片消息
*/
@Override
public boolean supportsPartialMessages() {
return false;
}
}
package com.demo.websocket.util;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
public class WebSocketSessionUtils {
/**
* session缓存
*/
private static Map sessions = new ConcurrentHashMap<>();
/**
* 保存连接
*/
public static void add(String sessionId, WebSocketSession session) {
sessions.put(sessionId, session);
}
/**
* 获取连接
*/
public static WebSocketSession get(String sessionId) {
return sessions.get(sessionId);
}
/**
* 移除连接
*/
public static void remove(String sessionId) {
sessions.remove(sessionId);
}
/**
* 清空连接
*/
public static void clear() {
sessions.clear();
}
/**
* 获取连接数
*/
public static int getconnections() {
return sessions.size();
}
/**
* 是否存在
*/
public static boolean exists(String sessionId) {
if (sessions.containsKey(sessionId)) {
return true;
}
return false;
}
/**
* 发送消息到客户端
*/
public static void sendMessage(String sessionId, String message) {
if (sessionId == null || !exists(sessionId)) {
throw new NullPointerException(sessionId);
}
WebSocketSession session = get(sessionId);
try {
session.sendMessage(new TextMessage(message));
} catch (IOException e) {
remove(sessionId);
e.printStackTrace();
}
}
/**
* 获得UUID
*/
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}
org.springframework
spring-webmvc
4.3.18.RELEASE
org.springframework
spring-websocket
4.3.18.RELEASE
location / {
proxy_read_timeout 3600s;
}