package com.apobates.forum.thrones;

import com.apobates.forum.thrones.controller.helper.MessageSocketPushHandler;
import com.apobates.forum.thrones.controller.helper.TopicSocketUpdatedHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置文件类
 *
 * @author xiaofanku
 * @since 20200527
 */
@Configuration
@EnableWebSocket
@PropertySource(value = "classpath:global.properties", ignoreResourceNotFound = true, encoding = "UTF-8")
public class WebSocketConfig implements WebSocketConfigurer {
    @Value("${site.domain}")
    private String siteDomain;
    
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(topicUpdatedHandler(), "/sock/updated").setAllowedOrigins(siteDomain).withSockJS();
        registry.addHandler(messageUpdatedHandler(), "/sock/message").setAllowedOrigins(siteDomain).withSockJS();
    }
    //WebSocket
    @Bean(name = "topicUpdatedHandler")
    public TopicSocketUpdatedHandler topicUpdatedHandler() {
        return new com.apobates.forum.thrones.controller.helper.TopicSocketUpdatedHandler();
    }
    
    @Bean(name = "messageUpdatedHandler")
    public MessageSocketPushHandler messageUpdatedHandler() {
        return new com.apobates.forum.thrones.controller.helper.MessageSocketPushHandler();
    }
}