spring与websocket

Java / 2020-05-05

四个类:

管理所有session的类:

package cn.redarm.springbootstudy.model;

import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author redarm
 * @Date 2020/5/5 7:43 下午
 **/
public class WebSocketSessionManager {
    
    private static ConcurrentHashMap<String, WebSocketSession> manager = new ConcurrentHashMap<>();
    
    /**
     * @Author redarm
     * @Description //TODO add a session to manager
     * @Date 7:45 下午 2020/5/5
     * @Param [key, webSocketSession]
     * @return void
     **/
    public void add(String key, WebSocketSession webSocketSession){
        manager.put(key, webSocketSession);
    }
    
    /**
     * @Author redarm
     * @Description //TODO get a session by key
     * @Date 7:46 下午 2020/5/5
     * @Param [key]
     * @return org.springframework.web.socket.WebSocketSession
     **/
    public WebSocketSession get(String key){
        return manager.get(key);
    }
    
    /**
     * @Author redarm
     * @Description //TODO remove session by key
     * @Date 7:46 下午 2020/5/5
     * @Param [key]
     * @return org.springframework.web.socket.WebSocketSession
     **/
    public WebSocketSession remove(String key){
        return manager.remove(key);
    }
}

处理连接,text消息,断开连接的类

package cn.redarm.springbootstudy.compoment;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @Author redarm
 * @Date 2020/5/5 7:48 下午
 **/
@Component
public class WebsocketTextHandler extends TextWebSocketHandler {

    /**
     * @Author redarm
     * @Description //TODO 当有session连接后执行的方法
     * @Date 7:50 下午 2020/5/5
     * @Param [session]
     * @return void
     **/
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
    }

    /**
     * @Author redarm
     * @Description //TODO 收到消息后处理的方法
     * @Date 7:51 下午 2020/5/5
     * @Param [session, message]
     * @return void
     **/
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        super.handleMessage(session, message);
    }

    /**
     * @Author redarm
     * @Description //TODO 连接断开执行的方法
     * @Date 7:51 下午 2020/5/5
     * @Param [session, status]
     * @return void
     **/
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
    }
}


处理连接之前的握手:

package cn.redarm.springbootstudy.compoment;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

/**
 * @Author redarm
 * @Date 2020/5/5 7:51 下午
 **/
@Component
public class WebsocketHandshake implements HandshakeInterceptor {
    /**
     * @Author redarm
     * @Description //TODO 当连接之前的握手之前
     * @Date 7:52 下午 2020/5/5
     * @Param [serverHttpRequest, serverHttpResponse, webSocketHandler, map]
     * @return boolean
     **/
    @Override
    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
        return false;
    }

    /**
     * @Author redarm
     * @Description //TODO 连接之前的握手之后
     * @Date 7:52 下午 2020/5/5
     * @Param [serverHttpRequest, serverHttpResponse, webSocketHandler, e]
     * @return void
     **/
    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {

    }
}

配置websocket类,把处理消息和处理握手的类加进去,并允许所有跨域连接

package cn.redarm.springbootstudy.config;

import cn.redarm.springbootstudy.compoment.WebsocketHandshake;
import cn.redarm.springbootstudy.compoment.WebsocketTextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @Author redarm
 * @Date 2020/5/5 7:53 下午
 **/
@Configuration
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebsocketHandshake websocketHandshake;

    @Autowired
    private WebsocketTextHandler websocketTextHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry
                .addHandler(websocketTextHandler)
                .addInterceptors(websocketHandshake)
                .setAllowedOrigins("*");
    }
}