xiaoyong931011
2021-12-08 f5e6133809c553cfd9fb28ee61019927c547c374
src/main/java/com/xcong/excoin/configurations/WebSocketConfig.java
@@ -1,11 +1,26 @@
package com.xcong.excoin.configurations;
import com.xcong.excoin.websocket.handler.FishHitWebSocketHandler;
import com.xcong.excoin.websocket.handler.FishHitWebSocketHandshakeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
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.ServerEndpointExporter;
//@Configuration
public class WebSocketConfig {
@Slf4j
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private FishHitWebSocketHandler fishHitWebSocketHandler;
    @Autowired
    private FishHitWebSocketHandshakeInterceptor fishHitWebSocketHandshakeInterceptor;
    /**
     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
     */
@@ -13,4 +28,11 @@
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(fishHitWebSocketHandler, "websocket/fish/hit")
                .setAllowedOrigins("*")
                .addInterceptors(fishHitWebSocketHandshakeInterceptor);
    }
}