Helius
2021-12-03 31702fe7fb4a4576daf0420539ff3ccc1216c6fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
 
@Slf4j
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
 
    @Autowired
    private FishHitWebSocketHandler fishHitWebSocketHandler;
    @Autowired
    private FishHitWebSocketHandshakeInterceptor fishHitWebSocketHandshakeInterceptor;
 
    /**
     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
 
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(fishHitWebSocketHandler, "websocket/fish/hit")
                .setAllowedOrigins("*")
                .addInterceptors(fishHitWebSocketHandshakeInterceptor);
    }
}