package cn.com.ty.lift.push.websocket.config;

import cn.com.ty.lift.push.message.service.TimeMessageService;
import cn.com.ty.lift.push.websocket.handler.TimeyMessageHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
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 javax.annotation.Resource;
import java.util.concurrent.Executors;

/**
 * @ClassName WebSocketConfig
 * @Description 消息推动配置
 * @Author huangyuan
 * @Date 2019-04-19 22:53
 **/
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private TimeMessageService timeMessageService;

    @Bean
    public TaskScheduler taskScheduler() {
        return new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor());
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new TimeyMessageHandler(timeMessageService), "/myMessageHandler")
                .setAllowedOrigins("*")
                .addInterceptors(new WebSocketInterceptor());
    }
}
